1 / 19

Class And Object

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;

hinda
Télécharger la présentation

Class And Object

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 4 Class And Object

  2. Session Objectives • Define Class & Objects • Private ,Public Access Specifier • How to Create a Object • How to call the member Function • Scope Resolution Operator

  3. 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,..>;

  4. For Example : class emp { private: char name[25]; //Member Data int empno; float salary; public: void get_data() // Member Function { --------- } void show_details() { ------------- ------------ };

  5. objectname.func_name(arguments); How to call the function from main()

  6. 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; }

  7. 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

  8. 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; } };

  9. 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; }

  10. 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(); }

  11. 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(); } }

  12. 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; }

  13. 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

  14. 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; }

  15. 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(); }

  16. 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.

  17. 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?

More Related