Delhi Public School, R.K.

Puram

Computer

Science Class XII

Class & Objects
A class is used to encapsulate data and functions in a single unit. Contents of a class are known as members of the class; data inside the class is known as data member and function included inside the class is known as member function. Members of the class are private by default. (i.e. they are not accessible outside the scope of the class). An Object has the similar relationship to a class that a variable has to a data type. An object is said to be an instance of a class, in the same way 'YOU' and 'ME' are the instances of a class 'HUMAN'. Class may contain data as well as functions.
r--------."

Class
Data Members

-

-

int

Student
Rna;

+
Member Function
~ un Kapur

- -- --

char Namef201; Data void Input () ;

Encapsulation

void Output 0 ;

A class with no instances (no objects) is know!') as an abstract class and a class having objects is known as a concrete class. Every object has its identity, state and behaviour. Identity is the property of the object, which distinguishes the object from other objects. Static and dynamic properties associated with the object signify the stat9 of the object. The operations/functions associated with the object exhibit the behaviour of the object. A class has three visibiiity modes for keeping its members:

private

X 1-----...............
I11"1(

Object) private members - not accessible to objects protected members - not accessible to objects

protected

l----.._"'"'1.~( Object) 1 ~--'_--I"IIIi"l(Object]
Programming

public
\_._------'

V

X

public members - accessible to objects #1

Object Oriented

By

Mukesh Kumar REF: DPSRlCOMP12/2012-2013107

void SHOW DATA(). member-function.} //Semicolon is must //memberfunctions definition Hospital: :Register() //outside the class cout«"Patient No?".cin. protected and public. R. void { //Public member function prototype //Public memberfunctions definition void ShowStatus() { cout«Pno<<Name<<wardno<<endl.Max).P:Jram Computer Science Class XII In C++. public: void Register (). Members in public visibility mode of the class are accessible to the objects of the class whereas members in private v. char Name [20] .sibifity mode are not accessible to the objects of class (are only accessible inside the class to tne member functions of the class). cout«"Name ?".h> const int Max'="2 . Wardno.the-objects. member-function. cout«"Ward No ?". //Private data members }. class ANIMAL Data Members { int LEG. 0 class Hospital { in t Pno.EYE.Delhi Public Zchool. member-function.getline(Name. public: data-member. Following C++ program illustrates the use of class and objects: #include <iostream. l Object Declaration Members of class can be kept In any of the three visibility modes private.//or gets (Neme)./IH: Herbivore I/O: Omnivore public: void ENT DATA().cin»Pno.K.EAR. char TYPE. ANIMAL }. protected: data-member. Note: Protected members will be explained in the inheritance topic. a class can be defined with the following syntax: class Class-Name data-member. l. Class_Name List-of. //Semicolon not reqaired Object Oriented Programming By Mukesh Kumar REF: DPSRlCOMP12i2012-2013/07 #2 .»Wardno.cin.

one can see that a class Hospital is defined with three private data members Pno. In the above example object P1 occupies 24 bytes.Puram void { Computer Science Class XII main() Hospital Pl. Member function can be defined inside the class as well as outside the class.int M) void Disp(int { I I Function 3 for } (int I=l. //cal1 for Function 2 . where the functions having same name with different set of parameters perform different operations. Both of these functions can be called from the objects of the class in the same way. Register () i Pl. Polymorphism: The process of using an operator or a function in different ways for different set of inputs given is known as polymorphism.y) ii/call for Function Disp().I++) cout«I«endl. Wardno and Name.P2 of Hospital IIMember vJnction call for the object P1 IIMember function call for the object P2 **NOT ACCESSIBLE member **NOT ACCESSIBLE as Wardno is a private member as Pno is a private } In the above example. R. Functions with control structures should be defined outside the class.Register() i P2. Generally. ShowStatus ().Prints numbers from 1 to 10 table of 5 upto 10 multiples Hello Object Oriented Programming By Mukesh Kumar' REF: DPSRlCOMP12/2012-2013!07 #3 . The size of object (in bytes) depends upon the data members present in the class it belongs to. Function overloading is an example of polymorphism.Prints 1 . and two public member functions RegisterO and ShowStatusO.Wardnoi II Ilcin»P2.I++) cout<<N<<"x"<<I<<":="<<N*r<<endl.Pno.Prints 3 .y=lO.I<=N.P2. as shown in the example. Pl.Oelhi Public school. P2. llDeclaration of the objects P1.I<=M. member function ShowStatusO is defined inside the class whereas member function Register{) is defined outside the class by using scope resolution operator (::) with the name of the class it belongs to. Example: void Disp() { //Function 1 cout«"Hello"«endl. Ilcall for Function Disp(x. Disp(x). N. ShowStatus () i Ilcin»Pl. single line functions are defined inside the class and multiple line functions are defined outside the class to have clarity in understanding the behaviour of the class. void Disp{int { N) IIFunction 2 for (int I=l.K. void main () { int x=5.

Object Oriented Programming By Mukesh Kumar REF: DPSRlCOMP12f2012-2013/07 #4 . It has no return type.DispO T.} void Down{) {a-=5. public: Work() {X=10.Y. R. It is defined in public visibility mode 4. public: Trial() {a=O.Y=30. constructor can be overloaded. It is automatically called and executed at the time of creation/decbration c:f the object 5.b=O.b.Disp() } ----================:::r-i ~_ Automatic Call for Constructor () .Puram Cornput A Science Ciass XII Constructor: It is a special member function of class with the following unique features: 1.} }.Disp T.}IIConstructor Work(int C) I/Constructor { 1 2 (Parameterized Constructor) X=C. 2. T . //Example class Trial { of Constructor int a.T.K.} void Disp () { -----==================::::C constructor] cout«a<<b<<endli void Raise (){a+=10 .Delhi Public School.b+=10.RaiseO //Example of Constructor class Work { Overloading int X. i J _ T. It has same name as the name of the class they belong to.y=ty. 3. ).b-=5.Y=2*Ci Work(int { tx. Moreover.DownO. . void main() { Trial T.int ty) //Constructor 3 (Parameterized Constructor) x=tx.

} void Play:: Disp () { cout«Count. } Play: :Play() ( [same as Play R(Ql . vo:i. void main() ( Play p.DispO. Object Oriented Programming By Mukesh Kumar REF: DPSRlCOMP12/2012-2013/07 #5 .Number. IICall IICal1 IICall of constructor of constructor of constructor 1 3 2 A copy constructor is an overloaded constructor function in which (an) object(s) of the same class is/are passed as a reference parameter(s). void Change (int.int) . cout<<Nwnber<<end.Change{90. Nwnber=P.DispO. IICall for constructor P. Play::Play(Play { &P) Ilcopy constructor Count=P. ).K.80) . It is used when an object's data value is related to or is initialised using another object's data value of the same class. //constructor Play(Play &) . Play Q(Pl.d Play.) /Iconstructor COunt=Oi Nwnber=O.Disp().Number+20. In the following example the values of data members of object Q are dependent on the values of data members of object P. /ICopy constructor call R. Play R=Q. I/Example. public: Play(}.Puram Computer Science Class XII Work Wi Work R(30.Delhi Public Schoolt void mainO { R.: Change (int C in t I N) { Count=C.of class Play { Copy Constructor int Count. P.l. IICopy constructor call Q.//copy constructor void Disp(l. Nwnber=N.Count+l0.60)i Work RR(30).

------1. . The most important aspect of inheritance is that it allows reusability 01 code. after classes themselves. T e oerlved classes not only inherit capabilities of the base class but also can aod new features of their own. Inheritance is a process of creating new classes (derived classes) from existing classes (base classes). Object Base Class private protected Public Access Specifier _Qrivate Qrotected public private _Qfotected _Qublic private protected public Derived Class Not Accessible private protected private protected _public Object Oriented Programming By Mukesh Kumar REF: DPSRlCOMP1212012-2013107 #6 . ~ e sability of code saves money as well as time and increase program reliatility. and also a debugged class can be adapted to work in different situations. The process of Inheritance does not affect the base class. RK.Puram Inheritance: Computer Science Class XII Inheritance is most powerful feature of Object Oriented Programming..elhi Public School. Visibility under various modes ~p_r_iv_a_t_e_M_e_m_b~e_r~------------~---)(~--_'~~... . x ~I Derived ceass Object Derived Class r---c~_ Pu blicMember 1-------------. In~eritance is very useful in original conceptualization and design of a programming problem.

Base Class class { State :public Country in t public: }.----~~==~~--~-------Delhi Public School. Derived Class void Get () .void Put () .mbers>. public: void Provide () .Puram computer Science Class XII T Single Level Inheritance Multi level Inheritance es of Inheritance Hierarchical Inheritance Multiple Inheritance Hvbrid Inheritance class { <Base <Class Class Name> Syntax (Single Level Inheritance) Members>: Members>. }. R. protected: int Infrastructure. M. No of Farms.K.embers>. Me. class <Derived <Class Name>:<Access Specifier> <Base Class Name> Members>. Object Oriented Programming By Mukesh Kumar REF: DPSRJCQMP1212012·2013/07 #7 . Members>: Class protected: <Class public: <Class }.void Collect () .. protected: <Class Country public: <Class }i State class { Country Example (Single level Inheritance) ill t ArmyFunds.

}. Object Oriented Programming By Mukesh Kumar REF: OPSRlCOMP12i2012-2013107 #8 . class { country Example (Multi level Inheritance) in t ArmyFunds. public: <Class Me.smbers>. class <Derived Class Name3>:<Access <Class Members>. }.K. }.mbers>.void Put ()..r-----------------------------Delhi Public School. class <Base Class Namel> __ --------------------_ Syntax <Class Members>: protected: <Class Members>. class District:public { State Derived Class int No of Offices. }. clas s <Derived/Ease { Class Name2>: <Access Specifier> <Ease Clas-'"NameD <Class Members>: protected: <Class Members>: public: <Class ..Members>.. J.Puram Computer Science Ciass XI! l. Specifier> <Ease Class protected: <Class Members>. public: void Input () . protected: int Infrastruc~ure. (Multi Level Inheritance) public: <Class J:-".. public: void Get (). }.". R. public: void Provide () rvo i. Base Class class { State:public Country J Derived/Base Class int No of Farms.d Collect () .void Output () .

public: <Class'Mem. R. PutO i country Derived Class class UnionTerritory:public { int public: void }.void Derived Class outpu t () . t. Specifier> <Base Class Name> protected: <Class Members>. <Class Members>.Delhi Public School. No_o£_Of£ices. Computer Science Class XII Syntax (H iera rch ica I Inheritance) protected: <Class Members>. }..void }. public: <Class Members>. class <Derived Class Namel>:<Access { Specifier> <Base Class Name> <Class Members>. clas s State: public Country { in t No of Farms.void Collect (). Base Class public: void Provide () . Object Oriented Programming By Mukesh Kumar REF: DPSRlCOMP1212012-2013/07 #9 . }. Input ().bers>. class <Derived Class Name2>:<Access <Class Members>.Puram class <Base Class Name> <Class Members>. protected: <Class Members>.K. class country { Example in t ArmyFunds. protected: int Infrastructure. public: } . public: void GetO .

Put () .mbers>. <Access Specifier> <Base Class Name2> <Class Members>: protected: <Class Members>. public: J.void Display () i class { School SId. Base Class 1 () . Object Oriented Programming By Mukesh Kumar REF: DPSR/CQMP1212012·2013!07 #10 .private School long public: void }. public: <Class Members>.Delhi Public School. class { <Base Class Name2> <Class Members>. }. public: void Register }.void Display().mbers>: }. Board. class { Board Example (Multiple Inheritance) long No. protected: float Budget. char Name [20] . Base Glass 2 Get () . (M u It! pie Inheritan ce) public: <Class Me.K. Derived Class public: void Enroll() . R. protected: <Class Me. clas s <Derived <Class Members>. }. Computer Science Class XII Syntax protected: <Class Members>. void class ( Student:public int AdmNo.Puram class <Base Class Namel>_----------_ <Class Members>. Cl ass Name>: <Acces s Speci f ier> <Base Cl as s Name 1> .

public: void Havei void Givei }. which can be accessed from the objects of. void Display () . class { p:private T int X2. which can be accessed by the object of ciass U. protected: int Y2.f Delhi Public School. protected: int Y. public: int Zl. void Change () . int Z2. a) b) c) d) e) f) g) Name the member function(s). Write the name of Base Class and Derived Class of Class T. Programming Object Oriented By Mukesh Kumar REF: DPSRiCOMP1212012-2013107 #11 . t () . void Calc () . public: . RK. class P. void Addi t () . which can be accessed from the member functions of class T. Name the data member(s). void Doit() . }. Name the data member(s). class { T:public U int Xl. void Showi t () . Is the data member Y declared in class U accessible to the member function Change() of class P? How many bytes will be required by an object belonging to Class P? Write the definition of member function DisplayO of class P to display all data members which are accessible from it.Puram Work Sheet Computer Science Class XII Answer the questions with the help of the portion of program shown below: class { u int x. }. t () . protected: int Yl.

Object Oriented Programming By Mukesi1 Kumar REF: OPSRiCOMP12/2012·2C13/07 #12 . R. Item Destroyed . destructor functions can NOT be overloaded."«endl. Data Hiding: Keeping the data in private/protected visibility mode in a class to prevent it from accidental modification (change) is known as data hiding. i there is a possibility of accidental I change in data.C++) { Item J. Destructor: It is a special member function of class with the following unique features: 1. lt is defined in public visibility mode 4. It is automatically called and executed when scope of an object gets over 5. !/Example of Destructor class Item { public: Item() {cout«"Item _TtemO }. Manufactured . I. Data Encapsulation _possible Procedural Programming Emphasis is on doing things (Functions) Follows top-down approach in program design i Due to presence of global variables..Delhi Public School. 2.C<2..Puram computer Scie ~ce Class XII Object Oriented Programming & Procedural Programming Object Oriented Programming Emphasis is on data Follows bottom-up approach in I jJ_ro_gram desl9_n Concept of Data Hiding prevents accidental change in the data Polymorphism.. Inheritance...-'" Item I. for (int C=O. void Working (){cout«" Item is Working"«en=d:::..Working() . J. It has same name as the name preceded by a symbol .. 3.(tilde)...K. we encapsulate the data and function together in a single unit.Working() . Moreover. ~_---""' {cout«"Item void mainO { Destructor __.} destroyed ..1."«end1. j Not Applicable ! ! J I Data Encapsulation: Wrapping up of data and functions in a single unit is known as data encapsulation.. It has no return type. In a class. Item Manufactured Item is Working Item Destroyed .. JII Call of Destructor for J }II Call of Destructor for I OUTPUT Item Manufactured Item is Working Item Manufactured Item is Working Item Destroyed .

Sign up to vote on this title
UsefulNot useful