You are on page 1of 19
1. class 2. object 3. inheritence 4. polymorphism 5. encapsuation 6. data binding... L.class: > class is a blue print. >class 1s the combination of data members(variables,constants....)and member functions. nelass is pot haveing menory space, >class is haveing menory .when related object is created. peach and every class 1s crated with the help of class keyword. peach and every class is terminated with seni colon(;)« synta class class name data menbers; nenber functions; sphere class name is an identifier. ex: glass sample acess specifier data type variable name; void FO body of the function shere acess specifier is public,private,protected. >by defult public is a acess specifier. sample progranm of clas: #include #include Class student { public: char name; char fname; vid display() cout<<"enter name of the student : Cin>>name; cout<<"enter father name of the student Cin>> fname; cout<<"enter htno of the student : cinn>hen cout<<"enter m1 marks of the student Page 1 cinsom]; cout." enter pl marks of the student cin>>pli cout<<"enter csi marks of the student Cinssesl; COURT EO CEE E ERE EERE EAD coutes"student information are Coutes student name 18 " . Coutse" student father nme is. (Anse fname Coutse student mL marks basco Amy cam fiteestudent pl marks dass... 06 AD" si foutscrstudent © i TAn Fees; coutse”student cst marks ia void main) { arserO: student obj; obj .displayG geechO: >object is an instance of a class syntax Classnane objectnane; ex sample obj; AHS EEL EEE EEE CEE CERES EERE EERE EEE EEE EERIE FE rE EE ** polymorphism: >polynorphisn 15 a greek world >'poly means many and morphism means forms. : ‘ > more then a function with same name and different works is a polymorphism. polymorphism is divided in 2 types. static polymorphism pace at conpile time that is called as static > to allocating the menory. si led as early binding polymorphism. polymorphism. it is also ca ex: function overloading #885%80perator overloading 2.dynanic polymorphism “sto allocating the menory space at run time that is called as dynamic polymorphism. it is also called as late binding polymorphism. ex: virtual function + operator overloadin operator: * operator is a special synbol. it is acting as a sone work. operator overloading means sane symbol act as a different works, e 10+20=30 —"sphoorthy"#"degree"=sphoorthydegree Page 2 cat return type operatornane operatorsymbol (argument Vist) } body of the operator overloading #includeciostrean.h> #includesconio.h> class a public: int x; public: yord AGine 99 xy 3 void operator +() + ps yoid display( cout<to conerting convertion. r73ex! 9. int float t t=a;//t=10.0 =->type conertion is divided into 4 types. = converting from base type to base type 2 converting from base type to class type [converting from class type to base type 2 converting from class type to class type =->these 4 type convertions is in. two ways. 1. implicit type convertion(conpiler is convert from one type to anthor type) 2! explicit type convertion(progranier is convert from one type to anthor type) 1. converting from base type to base typ >to conver a variable value from base data type to anthor base data type Page 3 on <-st0 conver a variable value from base data type to Class type Hincludestost ream. Wincludesconto,he class A { public: int x5 public: viod ACfloat b) xebi coutce “x value is 2"< #include class & { public: int x public: yord Aint 9) xy yord operator +0) ex; Daa: yord etsptavO cout< a function is available inside ofthe class 1s called as function in inside of the class. ex: #includesiostream.h> #include class A publi int Couts<"enter x vlaue:"; cins\; courceenter y value:"; cinssyj gouts<"the addition of x,y values are:"c«xty; Void main() clrser a ob}0) 0b} .cisplayO: geren: 2. function in outside of the class of the class is called as function in outside ‘unction is availa of the class. syntax: ‘scope resolution operator) functionnane(parameters) feturn type’ classname body of the function; #includeciostream.h> #includecconio.h> Class & public: int x; int y; public: int display();//declaration of the function i Aix display) Coute<"enter x value:"; cins>xy coute<"enter y value cins>y; cout«<"the addition of x,y values ar void main() cIrser(); & obj); obj.displayO; Page 5 geen: united nheritence: TShavaringcchifting) the properties from parent class to child class or base Class to derived class or super class to sub class ScSnhen ever we are using inheritence reusable of progranm code Sisnhen ever we are using inheritence reduce the progranm code syntax: Class childclass name:acessspecifier class pareatclassnane ; body of the child class rie a //parent class or base class or super class inlQ { hao i i fun30, i } i class b:public class a//child class or derived class or sub class i funlQ funs { I; -->¢4s can be supported 5 types inheritenceses + simple inheritence D multi level inheritence {multiple inheritence ! hirarchical inheritence 1) hybried inheritence sinple inheritence: =->simple inheritence means only one parent class or base class or super class and only one child class derived class or sub class ex: parent class A child class Page 6 ce #include #includecconio.h> Glass A //parent class public: yotd show) "cca; Couts<"the given a value is "ecb} coutce"the given b value is class B:public class A//child class. { public: int c=30; int d=40; pubis yord displayO Coutc<"the given c value is:"<in this inheritence multiple parenet classes and multiple child classes is a multi Tevel inheritence . =-> in this inheritence middi] level classes are acting as a parent and child Classes cx: parent class x sub parent class// acting as a parent and child classes R sub parent class// acting as a parent and child classes a child class ex #includeciostream.h> #include class grandfather Page 7 oe ¢ public: int x=10 int y=20; public yord show() Coutec"the given x value 1s coutex"the given y value 18! Glass father:public grandfather//acting as a parent and child Class public: ant 1930; int j=40: public: yord shot Coutex"the given i value is: coutec"the given } value 4 class child:public father i public ‘int k=50; 3nt 1608 public {oid shoo coutec"the given k value i goutee"the given 1 value 1s! iid rain) { cirserQi child 05) 0b; 0b} 'shon(); father, ob}10% objl-shonlO; obj1.shon(; objl.show20 ;//invalid grandfather 0bj20; 0bj2.show( 05}2.shoal();//invalid 0092, shon20)://invalid geechO: 3, multiple inheritence: ‘acquaring(shiffting) the properties from multiple parent classes to one child class 15 a multiple inheritence. ex: class parent] class parent2 Class parent3.cve parentn //parent classes sels oe class child ex: #includeciostream.h> #include glass parent public: int int be public yord stow() 0; Cout<<"the given a value is Gout<<"the given b value is class parent2 public int int public: yor shovt() Cout<<"the given k value i gout<<"the given 1 class child:public parent1, public parent2 public: int x=50; int yoid show) Cout<<"the given x value is:"cex; gout<<"the given y value is:"ccy: void main() cirserQ; ob} ‘show? parent obj10); 0bj1. show(! ob41 show); //invatid obj1. show?) //invlaid parent? obj20; EEE stearate 10 ob toa} 3 obj2. show! 9b)2 show invalid 4. hirarchical inheritence: > agvaring(ahifFeing) properties from one parent class to nore the one chilé Classes is called as hirarchical inheritence ox: parent class Page 9 chilld classi child class? classn ex: #include #includesconio.h> class parent { public: int a=1o: int bo20} public: Void show() Coutssthe given a value ig: "< the combination of any two or more inheritences is called as hybried coe Parent class s>child2 class +s. sehiTd3 classe. ex: #includeciostream.h» class parent public: int a=10; int b=20} public: pe show() Cout<<"the given a vlaue is:"< this is default access specifiers. actyen ever 82 are using public access specifrier all data renbers and functions are shiffing from parent class to chil clace Glass 8: public class A // this b class is haveing pia.xy { int x=20; 2. private: steteeeeead . 7» when ever we are using private access specifrier all data members and functions are not shiffing from parent class to child class Page 12 7 coe class 8: public class a // this b clas. Int x=20; Hi y=30; sis haveing only x.y £1855 € 7/7 p.q are not eligible Int ke100; ot 1200; 3. protecte: ecifrier all data menbers and -> when ever we are using protected access =p i1d class. functions are shiffing from parent class to ¢ x class a Protected: nt p=50; int qe60; Glass 8: public class A // this b class is haveing only p,q x,y Int x=20; int y=303 h glass © // p,q are not eligible Int ke1003 int 12200; ple functions in a progranm with sane nane and different arguments and Gifferent type of argunents is called as function overloading. ex ‘includeciostream.h> #include int show(); int shonCint aint b); int show(float p, float q); joe show) int eye aa coutec"enter x values"; cinz> x; cout<<"enter y value:"; cins>y; return’ 0; int shonint a, int b) Couc<"the addition of a,b values are return 0; i “< #includexconio,h> class parent public: publi yore Virtual shoo Coute<"the given a value is:" texplate is blue print or formula to defining function teaplate and class te. ore Page 14 co -> template is also cated as genaric function or genaric. class, => genaric means to work multiple data type values to a single form. > template is divided tn to bxo types. 1. function temlate 2. class template L.function template ss when ever ne are applaying genaric concept on function that is called as function template syntax: Template #includecconio.h> Template F sumer x. Ty) Sum= x+y; return sum; yoid main) clrserO; cout<<"the addition of x,y values are:"< when ever we are applaying genaric concept on class that is called as class template. syntax: Template Tenplate T class A a public: T Ty publics _" show() Cins>x>y; } sum(T x, T y) sum= x4 return sum; Page 15 void maint) cleserO: A in a single programs is haveing maultiple functions with same rane and different argurents 15 called as function overloading ==> overloading function is haveing texplate concept is called as overloading of function tenplates ex: Fincludeciostrean.h> nelude Tenplate pace Ty) sur x+y; return sun; } F sence xX) TYy, T 2) Sum xey44 return si doid nain() clrserQ; Cout<<"the addition of x,y values are:"<types of errors. s->in ces there are tho types of errors. 1. compile tine 2. run time 1. compile time:: > hen ever we are compile the programm that tire we are haveing ary grammer or syntax errors 1s called as conpile tine error. => these type of errors are rectify the programmer 2. run time > > when ever we are run the programm that tine we are haveing any errors is cated as run tise error. | --> these type of errors are not rectify the programmer. page 16 > exception means run tine errors. > at isaceror at the tine of Mow of exicution to the prog SAE GS dstrub the progran Mow of exict ion exception mandding ==> a proararmer 18 handle the exceptions that type of errors is called as exception handling | fr Enese exception is doing with the help of 3 types of Keywords try 2: throw 3. catch 1. tr > it is keyword. it is a block of code > this try block is haveing doutfull code or error accured code syntax: ry govefull bolck of code 2. throw: it is a keyword, it is used to throw the exception accured in the try block with the help of vaiue, syntax try { Couftfull the block of code throw value; ch: it is key nord, it is a block of code in this catch Blok is haveing solution of the try error code one try is haveing many no of catch blocks. with out try there is no catch blocks => with out catch program is haveing try block. syntax catch (datatype variable) solution of try error block of code #include #include void rainc) clrserQ) int a,b; coutce “enter a,b values:" Ciny>a>>b; try tre 0) TF (b= { throw 0; else Page 17 ce Coutce "the division of a,b values are:"« many nurber of try and catch blocks in exception handling is called as muleypie exception hand) ing, =-> one try block can Containing one or more catch blocks. =*> one try block may or may not catch blocks >"> with out try block there is no catch blocks #includeciostream,h> #include vyord main() int ny coute<"enter any number: cins>n; try { Tf (n==1) throw 10; catchCint 4) { Cout<<"here n==1 is an error: datch(Foat 3) doutcethere nae? is an errori"s atehCchar b cout<<"here ni: } Cout<" this is an exception handling concept” goute<"it is develored by chary re th =-> rethroning exception is possible,where we have an inner and outer try and catch statements =-2an exception to be thrown from inner catch block to ourter catch block is called as re throwing exception handling. Page 18 #includeciost rean.t> inelude pe main® int a1; try { try throw 10; } catchCint 1) { coutee"this is inner exception throw i; } } 5 garehcine n) doutcet'this is utter, exception"; gescnchO page 19

You might also like