CLASS & OBJECT

Information about CLASS & OBJECT

Published on November 10, 2009

Author: MLMEENA

Source: authorstream.com

Content

CLASS & OBJECT : CLASS & OBJECT BY:-ML MEENA PGT(CS) KVS KV VIKASPURI NEW DELHI INDIA Slide 2: Class: A Class is a user defined data type to implement an abstract object. Abstract means to hide the details. A Class is a combination of data and functions. Data is called as data members and functions are called as member functions. Slide 3: Data member or member functions may be public, private or protected. Public means data members or member functions defining inside the class can be used at outside the class.( in different class and in main function) Private means data members and member functions can’t be used outside the class. Protected means data member and member functions can be used in the same class and its derived class (at one level) (not in main function). Slide 4: PRIVATE PUBLIC Class Example (Problem) : Class Example (Problem) #include<iostream.h> #include<stdio.h> class student { int rollno; char name[20]; }; void main() { student s; cout<<“enter the rollno.:”; cin>>s.rollno; cout<<“enter the name:”; gets(s.name); cout<<“rollno:”<<s.rollno; cout<<“\nname:”; puts(s.name); } Class Example (Solution) : Class Example (Solution) #include<iostream.h> #include<stdio.h> class student { public: int rollno; char name[20]; }; void main() { student s; cout<<“enter the rollno.:”; cin>>s.rollno; cout<<“enter the name:”; gets(s.name); cout<<“rollno:”<<s.rollno; cout<<“\nname:”; puts(s.name); } Member Function Defining Inside the Class : Member Function Defining Inside the Class #include<iostream.h> #include<stdio.h> class student { int rollno; char name[20]; public: void getdata() { cout<<“enter the rollno.:”; cin>>rollno; cout<<“enter the name:”; gets(name); } void putdata() { cout<<“rollno:”<<rollno; cout<<“\nname:”; puts(name); } }; void main() { student s; s.getdata(); s.putdata(); } Data Members (Private : in this example) Member Functions (Public: in this example) Calling member function Member Function Defining Outside the Class : Member Function Defining Outside the Class #include<iostream.h> #include<stdio.h> class student { int rollno; char name[20]; public: void getdata(); void putdata(); }; void student :: getdata() { cout<<“enter the rollno.:”; cin>>rollno; cout<<“enter the name:”; gets(name); } void student: :: putdata() { cout<<“rollno:”<<rollno; cout<<“\nname:”; puts(name); } void main() { student s; s.getdata(); s.putdata(); } Array of Class Objects : Array of Class Objects Problem: #include<iostream.h> #include<stdio.h> class student { int rollno; char name[20]; public: void getdata(); void putdata(); }; void student :: getdata() { cout<<“enter the rollno.:”; cin>>rollno; cout<<“enter the name:”; gets(name); } void student: :: putdata() { cout<<“rollno:”<<rollno; cout<<“\nname:”; puts(name); } void main() { student s1,s2; s1.getdata(); s2.getdata(); s1.putdata(); s2.putdata(); } Slide 10: Solution: #include<iostream.h> #include<stdio.h> class student { int rollno; char name[20]; public: void getdata(); void putdata(); }; void student :: getdata() { cout<<“enter the rollno.:”; cin>>rollno; cout<<“enter the name:”; gets(name); } void student: :: putdata() { cout<<“rollno:”<<rollno; cout<<“\nname:”; puts(name); } void main() { student s[10]; int i: // index variable for ( i = 0; i< 10; i++) { s[i].getdata ( ); } for (i = 0 ; i < 10 ; i ++) { s[i].putdata( ); } } Memory Allocation of Object : Memory Allocation of Object class student { int rollno; char name[20]; int marks; }; student s; rollno – 2 bytes name- 20 bytes marks- 2 bytes 24 bytes s Passing Object : Passing Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); void sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } void Complex : : sum ( complex A, complex B) { real = A.real + B.real; imag= A.imag + B.imag; } void main( ) { Complex X,Y,Z; X.getdata( ); Y.getdata( ); Z.sum(x,y); Z.putdata( ); } Passing Object : Passing Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); void sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } void Complex : : sum ( Complex A, Complex B) { real = A.real + B.real; imag= A.imag + B.imag; } void main( ) { Complex X,Y,Z; X.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.putdata( ); } X Y Z Passing Object : Passing Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); void sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } void Complex : : sum ( Complex A, Complex B) { real = A.real + B.real; imag= A.imag + B.imag; } void main( ) { Complex X,Y,Z; X.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.putdata( ); } 5 6 7 8 X Y Z Passing Object : Passing Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); void sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } void Complex : : sum ( Complex A, Complex B) { real = A.real + B.real; imag= A.imag + B.imag; } void main( ) { Complex X,Y,Z; X.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.putdata( ); } 5 6 7 8 X Y Z 5 6 7 8 A B Passing Object : Passing Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); void sum(Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } void Complex : : sum ( Complex A, Complex B) { real = A.real + B.real; imag= A.imag + B.imag; } void main( ) { Complex X,Y,Z; X.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.putdata( ); } 5 6 7 8 12 14 X Y Z 5 6 7 8 A B + + = = Passing Object : Passing Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); void sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } void complex : : sum ( Complex A, Complex B) { real = A.real + B.real; imag= A.imag + B.imag; } void main( ) { Complex X,Y,Z; X.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.putdata( ); } 12 + 14 i 5 6 7 8 12 14 X Y Z 5 6 7 8 A B + + = = Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } X Y Z Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } 5 6 7 8 X Y Z Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } 5 6 7 8 X Y Z 7 8 B Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } 5 6 7 8 X Y Z 7 8 B Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } 5 6 7 8 X Y Z 7 8 B 12 14 temp Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } 5 6 7 8 X Y Z 7 8 B 12 14 temp Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } 5 6 7 8 12 14 X Y Z 7 8 B 12 14 temp Returning Object : Returning Object #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); Complex sum (Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex Complex : : sum (Complex B) { Complex temp; temp.real=real + B.real; temp.imag= imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= X.sum (Y); Z.putdata( ); } 12 + 14 i 5 6 7 8 12 14 X Y Z 7 8 B 12 14 temp Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } X Y Z Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } 5 6 7 8 X Y Z Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } 5 6 7 8 X Y Z 7 8 B 5 6 A Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } 5 6 7 8 X Y Z 7 8 B 5 6 A + + = = temp Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } 5 6 7 8 X Y Z 7 8 B 5 6 A 12 14 + + = = temp Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } 5 6 7 8 12 14 X Y Z 7 8 B 5 6 A 12 14 + + = = temp Friend Function : Friend Function #include<iostream.h> class Complex { float real, imag; public: void getdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); }; void Complex : : getdata( ) { cout<<“enter real part:”; cin>>real; cout<<“enter imaginary part:”; cin>>imag; } void Complex : : putdata( ) { if (imag>=0) cout<<real<<“+”<<imag<<“i”; else cout<<real<<imag<<“i”; } Complex sum (Complex A, Complex B) { Complex temp; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; return temp; } void main ( ) { Complex X, Y, Z; X.Getdata( ); Y. getdata( ); Z= sum (X,Y); Z.putdata( ); } 12 + 14 i 5 6 7 8 12 14 X Y Z 7 8 B 5 6 A 12 14 + + = = temp Define a class tour in C++ with the description given below: : Define a class tour in C++ with the description given below: Private members: tcode of type string Noofadults of type integer Noofkids of type integer Kilometers of type integer Totalfare of type float Public members: A constructor to assign initial values as follows: Tcode with the word “NULL” Noofadults as 0 Noofkids as 0 Kilometers as 0 Totalfare as 0 A function assignfare( ) which calculates and assigns the value of data member totalfare as follows: for each adult Fare (Rs.) For Kilometers 500 >=1000 300 < 1000 & >= 500 200 < 500 for each kid the above fare will be 50% of the fare mentioned in the above table for example: if kilometers is 850, noofadults =2 and noofkids = 3 then totalfare should be calculated as noofadults * 300 + noofkids * 150 I.e. 2 * 300 + 3 * 150 = 1050 A function entertour( ) to imput the values of the data members tcode, noofadults, noofkids and kilometers and invoke assignfare( ) function. A function showtour ( ) which displays the contents of all the data members for a tour. Slide 36: class tour { char tcode[15]; int noofadults; int noofkids; int kilometers; float totalfare; public: tour( ) { strcpy(tcode,”null”); noofadults=0; noofkids=0; kilometers=0; totalfare=0; } void assignfare( ) { if (kilometers>=1000) totalfare= 500 * noofadults + 250 * noofkids; else if (kilometers>=500) totalfare= 300 * noofadults + 150 * noofkids; else totalfare= 200 * noofadults + 100 * noofkids; } void entertour( ) { cout<<“enter tcode:”; gets(tcode); cout<<“enter noofadults:”; cin>>noofadults; cout<<“enter noofkids:”; cin>>noofkids; cout<<“enter kilometers=“; cin>>kilometers; } void showtour ( ) { cout<<“tcode=“<<tcode; cout<<“\nnumber of adults=“<<noofadults; cout<<“\nnumber of kids=“<<noofkids; cout<<“\nkilometers=“<<kilometers; cout<<“\ntotalfare=“<<totalfare; } }; Function Overloading (Problem) : Function Overloading (Problem) #include<iostream.h> int area (int x, int y) { return x + y; } float area (float x, float y) { return x + y; } void main ( ) { cout<<area(5,6); cout<<area(7.5, 4.3); } Function Overloading (Solution) : Function Overloading (Solution) #include<iostream.h> int area (int x, int y) { return x + y; } float area (float x, float y) { return x + y; } void main ( ) { int a=5,b=6; float c=7.5, d=4.3; cout<<area (a, b); cout<<area (c, d); } Define a class HOUSING in C++ with the following descriptions: (4) : Define a class HOUSING in C++ with the following descriptions: (4) private members: REG_NO integer (ranges 10-1000) NAME array of characters (string) TYPE character COST float Public members: function read_data( ) to read an object of HOUSING type. Function display ( ) to display the details of an object. Function draw_nos( ) to choose and display the details of 2 houses selected randomly from an array of 10 objects of type HOUSING. Use random function to generate the registration nos. to match with REG_NO from the array. Slide 40: class HOUSING { int REG_NO; char NAME[20], TYPE; float COST; public: void read_data( ) { cout<<“Enter Registration Number=“; cin>>REG_NO; cout<<“Enter Name=“; gets(NAME); cout<<“Enter Type=“; cin>> TYPE; cout<<“Enter Cost=“; cin>>COST; } void display ( ) { cout<<“Registration No.”<<REG_NO; cout<<“\nName=“; puts(NAME); cout<<“\nType=“<<TYPE; cout<<“\nCost=“<<COST; } void draw_nos( ) { int no1, no2; randomize( ); no1=random(1991)+10; no2=random(1991)+10; for (int i=0; i<10; i++) if (arr[i].REG_NO==no1|| arr[i].REG_NO==no2) display(); } }; HOUSING arr[10];

Related presentations


Other presentations created by MLMEENA

costructor
22. 08. 2009
0 views

costructor