CPP
Class
& Object
Fill in the blanks
1.Class is Collection of object.
2.Object is Collection of data.
3.Access Specifiers are Public,Private & Protected.
4.How to access private variable In Public ->function
to access
5.Write a syntax for Object
Classname Objvariablename;
6.Write a Symbol for member function Outside of the class ::
Answer the following Question
(4
marks)
1.Write a Program for Accessing Private data
using Class & Object
#include<iostream.h>
#include<conio.h>
class train{
private:
char train_no[10];
char train_name[15];
float train_fare;
public:
void gettraindetails()
{
cout<<"enter train no , name , train
fare";
cin>>train_no>>train_name>>train_fare;
}
void printtraindetails()
{
cout<<"The Entered Train Details
are\n";
cout<<"----------------------------------\n";
cout<<"Train Number:"<<train_no<<endl;
cout<<"Train Name:"<<train_name<<endl;
cout<<"Train fare:"<<train_fare<<endl;
cout<<"----------------------------------\n";
}
};
void main()
{
train T;
clrscr();
T.gettraindetails();
T.printtraindetails();
getch();
}
2.Write a Program for Member function outside
of the Class
#include<iostream.h>
#include<conio.h>
class number{
private:
int n;
public:
void getnumber();
void printnumber()
{
cout<<"N="<<n<<"\n";
}
};
void number::getnumber()
{
cout<<"Get N";
cin>>n;
}
void main()
{
number T;
clrscr();
T.getnumber();
T.printnumber();
getch();
}
3.Write a Program for Nesting Member Function
#include<iostream.h>
#include<conio.h>
class set{
private:
int m,n;
public:
void display();
void getdata()
{
cout<<"Get M and N="<<endl;
cin>>m>>n;
}
int largest();
};
int set::largest()
{
if(m>n)
{
return m;
}
else
{
return n;
}
}
void set::display()
{
cout<<"Given no are"<<m<<endl<<n<<endl;
cout<<"Largest is="<<largest();
}
void main()
{
set T;
clrscr();
T.getdata();
T.largest();
T.display();
getch();
}
4.Write a Program for Array using Class
#include<iostream.h>
#include<conio.h>
class array{
private:
int a[10];
public:
void getarray();
void putarray();
};
void array::getarray()
{
cout<<"enter 5 array";
for(int i=0;i<5;i++)
cin>>a[i];
}
void array::putarray()
{
for(int i=0;i<5;i++)
cout<<a[i]<<endl;
}
void main()
{
array A;
clrscr();
A.getarray();
A.putarray();
getch();
}
5.Write a Program for Static Data Member
#include<iostream.h>
#include<conio.h>
class item
{
int n;
static int count;
public:
void getdata()
{
cout<<"get N";
cin>>n;
count++;
}
void getcount()
{
cout<<"Count="<<count<<"\n";
}
};
int item::count;
void main()
{
item a,b,c;
clrscr();
a.getcount();
b.getcount();
c.getcount();
a.getdata();
b.getdata();
c.getdata();
cout<<"After reading data: ";
a.getcount();
getch();
}
6.Write a Program for Array of object using Class
#include<iostream.h>
#include<conio.h>
class employee{
private:
int eno,sal;
public:
void getdetail()
{
cout<<"enter employee eno and salary";
cin>>eno>>sal;
}
void printdetails()
{
cout<<eno<<"\t"<<sal<<endl;
}
};
void main()
{
employee e[5];
clrscr();
for(int i=0;i<5;i++)
{
e[i].getdetail();
}
for(i=0;i<5;i++)
{
e[i].printdetails();
}
getch();
}
No comments:
Post a Comment