Class and Object1
Class and Object1
31/01/11
Outlines
Declaration of Class and Class Objects Access Specifiers Class with an array as data member
Declaration of Class
class identifiers { access Specifier : statements ; };
Example
class Cars { private : int carno; public : void Display() { cout<<carno<<endl;} void Insert() { cin>>carno;} };
Declaration of Objects
class Cars mycar; OR Cars mycar ;
Name of class
name of objects
Contd
Cars car1, car2, car3 ;
Name of class
Example
#include<iostream> using namespace std; class Market { public: void display() { cout<<Welcome To This Market<<endl ;
Contd..
cout<<Here is the price list<<endl ; } }; int main() { Market L1 ; L1.display(); return 0 ; }
Example
#include<iostream> using namespace std; class Market { public : void dicplay() ; };
Contd..
void Market :: display() { cout<<Welcome To This Market<<endl ; cout<<Here is the price list<<endl ; } int main() { Market L1 ; L1.display(); return 0 ; }
Contd..
#include<iostream> using namespace std; class Market { private : int x, y, z ; public : void display1() { cout<< Welcome , Here is the price list <<endl;}
Contd..
void Setprice(int a, int b, int c) { x = a, y = b, z = c ; } void display2() { cout << Price of item1 = << x<<endl ; cout << Price of item2 = << y<<endl ; cout << Price of item1 = << z<<endl ; } }; int main() { Market L1 ;
Contd..
L1.Setprice(4,6,2) ; L1. display1(); L1.display2(); } Note : put the definition function outside the class: void Market:: Setprice(int a, int b, int c) { x = a, y = b, z = c ; }
Contd..
int main () { Rect R1 ; R1.x = 8; R1.y = 5; cout << Area = << R1.Area()<<endl; cout << Length = << R1.x<<endl ; cout<< Width = << R1.y<< endl; return 0 ; }
Example
#include<iostream> using namespace std ; class Rect { private : int x, y ; public : void setsides( int L, int W){ x = L, y = W ; } int Area (){ return x*y ;} void dimension() { cout << Length = <<x<<\tWidth = <<y<<endl; } };
Contd..
int main () { Rect R1; R1.setsides(8,5); cout<<Area = <<R1.Area()<<endl ; R1.Dimension() ; return 0 ; }
Contd..
void Setprice (int x[4]) ; void display2() { for(int i = 0; i<4; i++) cout<< Price of item <<i+1<< = <<x*i]<<endl; }; void List ::Setprice(int a[4]) { for(int j= 0; j<4;j++) x[j] = a[j]; }
Contd..
void main() { List L1; int P[4] = {6,5,4,8} ; L1.Setprice(P); L1. Display1(); L1.Display2(); }
Contd..
void Setmarks(int x[4]) ; void Setnames(char Names[4][20]); void Display2() { for( int i = 0;i<4;i++) cout<<Marks obtained by <<Names*i+<< = <<x[i]<<endl; } };
Contd..
void List :: Setmarks(int a[4] ) { for( int j=0; j<4;j++) x[j] = a[j] ; } void List :: Setnames( char b[4][20]) { for( int m = 0 ; m<4; m++) { for( int p = 0 ; p<20 ; p++) Names [m][p] = b[m][p]; } }
Contd..
void main() { List L1; char Students [4][20]; cout<< Enter the names of 4 students:; for( int i = 0;i<4;i++) cin.getline(Students[i][20]); cout<<Enter Marks<<endl; int P[4]; for( int k = 0; k<4; k++) cin>>P[k]; { L1.Setmarks(P); L1.Setnames(Students); } L1.Display1() ; L1.Display2() ; }
Exercise
Wap to illustrate the preparation of marks list in 4 subjects and total marks. Wap in which you have to create a class Cubical with a method for initializing sides of cube, for calculating surface area, and volume. Through object of this class call them in main.