210 likes | 492 Views
CHAPTER 4. Class And Object. Session Objectives. Define Class & Objects. Private ,Public Access Specifier. How to Create a Object. How to call the member Function. Scope Resolution Operator. TO CREATE A NEW CLASS. SYNTAX : class<classname> { private : datatype member1;
E N D
CHAPTER 4 Class And Object
Session Objectives • Define Class & Objects • Private ,Public Access Specifier • How to Create a Object • How to call the member Function • Scope Resolution Operator
TO CREATE A NEW CLASS SYNTAX : class<classname> { private : datatype member1; datatype member2; public : <retu_type> func_name1(arguments); <retu_type> func_name2(arguments); }; create a object from the main()functions Syntax: classname<objectname1, objectname2,..>;
For Example : class emp { private: char name[25]; //Member Data int empno; float salary; public: void get_data() // Member Function { --------- } void show_details() { ------------- ------------ };
objectname.func_name(arguments); How to call the function from main()
Write a Program to calculate the area of a circle using class & Object #include<iostream.h> #include<conio.h> class raji { private: int radius; float area; public: void getdata() { cout<<"Enter the radius"<<endl; cin>>radius; }
void calculate() { area=3.14*radius*radius; } void showdata() { cout<<"The area of the circle is "<<area<<endl; } }; void main() { raji r; r.getdata(); r.calculate(); r.showdata(); getch(); } RESULT Enter the Radius : 2 The area of the circle is
Class Objects void main() { smallobj a1,a2; clrscr(); a1.setdata(2500); a2.setdata(4000); a1.showdata(); a2.showdata(); getch(); } #include<iostream.h> #include<conio.h> class smallobj { private : int s; public : void setdata(int d) { s=d; } void showdata() { cout<<endl<<"the Data is "<<s; } };
void showdist() { cout<<endl<<feet<<endl<<inches; } }; void main() { distance dist1,dist2; clrscr(); dist1.setdist(11,6.25); dist2.getdist(); dist1.showdist(); dist2.showdist(); getch(); } Class & Objects Example #include<iostream.h> #include<conio.h> class distance { private: int feet; float inches; public : void setdist(int ft,float in) { feet=ft; inches=in; } void getdist() { cout<<endl<<"Enter Feet and Inches Value:"; cin>>feet>>inches; }
ARRAY AS CLASS MEMBER DATA #include<iostream.h> #include<conio.h> const int MAX=100; class stack { private: int st[MAX]; int top; public : stack() { top=0; } void push(int v) { st [++top]=v; } int pop() { return st[top--]; } }; void main() { stack s1; s1.push(11); s1.push(22); cout<<" Ist Data :"<<s1.pop(); cout<<" IInd Data :"<<s1.pop(); s1.push(33); s1.push(44); s1.push(55); s1.push(66); cout<<" III Data :"<<s1.pop()<<endl; cout<<" IV Data :"<<s1.pop()<<endl; cout<<" V Data :"<<s1.pop()<<endl; cout<<" VI Data :"<<s1.pop()<<endl; getch(); }
ARRAY OF OBJECTS I #include<iostream.h> const int MAX=100; class Distance { private: int feet; float inches; public : void get() { cout<<"\n Enter Feet Value :"; cin>>feet; cout<<"\n Enter Inche Value :"; cin>>inches; } void show() { cout<<feet<<"\t"<<inches<<endl; } }; void main() { Distance dist[MAX]; int n=0; char ans; cout<<endl; do { cout<<"\n Enter Distance "<<n+1; dist[n++].get(); cout<<"\n Another Data [y/n]?"; cin>>ans; }while(ans != 'n'); for(int j=0;j<n;j++) { cout<<"Distance Number "<<j+1<<" Is "; dist[j].show(); } }
ARRAY OF OBJECTS II void calculate() { area=3.14*radius*radius; } void show() { cout<<"the area is "<<area<<endl; } }; void main() { int i; circle e[5]; for(i=0;i<5;i++) { e[i].getdata(); e[i].calculate(); e[i].show(); } getch(); } #include<iostream.h> #include<conio.h> class circle { private: int area, radius; public: void getdata() { cout<<"Enter the radius value"<<endl; cin>>radius; }
Output Enter the radius value 1 The area is 3 Enter the radius value 2 The area is 12 Enter the radius value 3 The area is 28 Enter the radius value 4 The area is 50 Enter the radius value 4 The area is 50
void circle::calculate() { area=3.14*radius*radius; } void circle::show() { cout<<"Area"<<area; } void main() { circle r; r.getdata(); r.calculate(); r.show(); r.get(100); r.calculate(); r.show(); } RESULT : ENTER THE RADIUS : 5 Area FOR 5=78.5 Area FOR 100=31400 Scope Resolution Operator (::) The member function of A program are defined outside the boundary of the class using the scope resolution operator(::) #include<iostream.h> class circle { private: int radius; float area; public: void getdata(); void get(int r); void calculate(); void show(); }; void circle::getdata(){ cout<<"RADIUS"<<endl; cin>>radius; } void circle::get(int r) { radius=r; }
Returning Values from class's Memeber Function to main() function #include<iostream.h> #include<conio.h> class rectangle { private : int length,breadth,area; public : void setdata(int a,int b) { length=a; breadth=b; } int calculate() { return length*breadth; } }; void main() { rectangle r; int area; clrscr(); r.setdata(5,8); area=r.calculate(); cout<<endl<<" Area is :"<<area; getch(); }
Summary • Each class specification starts with the keyword “class” • The Class Specification must always end with a semicolon (;) • Data abstraction is the ability to create user-defined data types for modeling real world objects using built-in data types. • Classes are used for data abstraction by hiding the implementation of a type in the “private” part.
EXERCISES Describe the use of Array of Objects? List the various applications of Scope Resloution Operator? Explain briefly Classes & Objects? Describe the Array as Class member Data?