CLASS &
OBJECT
CLASS:
Collections of object
syntax
class className {
// some data
// some functions
};
OBJECT:
Collections of data
ex. name,pen,object
Syntax
className object;
Access
Specifier
Public - Anyone can access
Private - Access with function
Protected - one class to another class access
Accessing
Private Data
#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(); }
output MEMBER FUNCTION OUTSIDE 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(); }
OUTPUT 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(); } output Recursive member function #include<iostream.h> #include<conio.h> class num{ public: int fact(int n) { if(n==0||n==1) { return 1; } else{ return
(n*fact(n-1)); }} }; void main() { num
N; clrscr(); cout<<N.fact(5); getch(); } Output 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(); } output Array
of object #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(); }
output Passing argument to member
function & Returning values #include<iostream.h> #include<conio.h> class item { int
itemno,price; public: void
getitemdetails(int i,int
j) { itemno=i; price=j; } int
cal_amount(int q) { return
q*price; } void
printdetails() { cout<<itemno<<price; } }; void main() { item K; clrscr(); K.getitemdetails(3,45); int
r=K.cal_amount(5); cout<<"Amount="<<r; getch(); } output Function
overloading #include<iostream.h> #include<conio.h> class item { int
itemno,price; public: void
getitemdetails() { cout<<"get item no and price"; cin>>itemno>>price; } void
getitemdetails(int i,int
j) { itemno=i; price=j; } int
cal_amount(int q) { return
q*price; } void
printdetails() { cout<<itemno<<price; } }; void main() { item K; clrscr(); K.getitemdetails(3,45); int
r=K.cal_amount(5); cout<<"Amount="<<r; getch(); } Output Pointer to object #include<iostream.h> #include<conio.h> class A { int a; public: void
getdata() { cout<<"get
no "; cin>>a; } void
display() { cout<<"Data="<<a; } }; void main() { A a,*b=&a; clrscr(); a.getdata(); a.display(); b->getdata(); b->display(); getch(); } output 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(); } output
|
||||
0 comments:
Post a Comment