SlideShare a Scribd company logo
1 of 57
OBJECT ORIENTED PROGRAMMING IN C++<br />#. INTRODUCTION TO OBJECT ORIENTED PROGRAMMING:<br />Object oriented program development is a new programming style having real world thinking. It is not a programming technique. So each and every programmer has his own way of thinking. <br />1.object<br />2.class<br />3.data abstracation<br />4.data encapsulation<br />5.inheritance<br />6.polymorphism<br />7.dynamic binding<br />8.message passing<br />Object:<br />An object is defined as an entity that contain data and its related functions. The functions operate on that data. The objects may be either physical or logical. <br />Class:<br />A class is defined as a collection of objects with same type of data and functions. The functions of the class should be defined. With the help of the class we can create number of objects of type class.<br />Data abstraction:<br />Abstraction is defined as a grouping of essential details and ignoring other details. Data abstraction is defined as a named collection of data that describes a data object in a class <br />Data encapsulation:<br />Encapsulation is a technique used to protect the informations in an object from other objects. In an object data and its functions are encapsulated into a single entity. Because of this other objects and programs cannot aces the data in an object directly. This concept is called data encapsulation or data hiding<br />Inheritance:<br />Inheritance is defined as sharing of attributes and functions among classes based on a hierarchical or sequential relationship<br />Main class or supper class<br />Derived class or sub class<br />Polymorphism:<br />A function is said to be polymorphic, if it is applied to many different classes with different operation.<br />Dynamic binding:<br />Binding is defined as the connection between the function call and its corresponding program code to be executed. There are 2 types of binding<br />1.static binding<br />2.dynamic binding<br />Static binding:the binding occurs during compilation time<br />Dynamic binding: the binding occurs during run time. This is also called late binding.<br />Message passing:<br />Message passing is a process of locating and executing a function in response to a message. Locating means matching the given message with the list of available functions.<br />#. Structure in c++<br />Include file<br />Class definition<br />Data declaration<br />Member function definition<br />Main function<br />Object declaration<br />Body of main function<br />#include<iostream.h>              ……………header file<br />class stu                                ……………class defintion<br />{<br />private:………………visibility modifier<br />int x,y;…………….data declaration<br />public:<br />void read()…………….member function <br />{<br />cout<<”enter the val”;<br />cin>>x>>y;<br />}<br />void print()<br />{<br />cout<<”x=”<<x;<br />cout<<”y=”<<y;<br />}<br />};<br />void main()…………….main function<br />{<br />stu a;…………..data(object) declaration<br />a.read();……….called member function<br />a.print();<br />}<br />#. Control structures<br />A large number of functions are used that pass messages, and process the data contained in objects. A function is set up to perform a task. When the task is complex, many different algorithms can be designed to achieve the same goal. Some are simple to comprehend, while others are not.<br />  1.Decision making (or) conditional statement<br />  2.Looping statement<br />Decision making statement<br />•DMS are used to skip or to execute a group of statements based on the result of some condition.<br />•The decision making statements are,<br />•(i) simple if statement<br />•(ii) if…else statement<br />•(iii) switch statement<br />Simple if statement:<br />•Simple if statement is used to execute or skip one statement or group of statements for a particular condition. <br />•The general form is<br />•If(test condition)<br />{<br />–Statement block;<br />}<br />Next statement;<br />  When this statement is executed, the computer first evaluates the value of the test condition. If the value is true, statement block and next statement are executed sequentially. If the value is false, statement block is skipped and execution starts from next statement.<br />Example:<br />•#include<iostream.h><br />•#include<conio.h><br />•Void main()<br />•{<br />–Int mark;<br />–Char grade;<br />–Clrscr();<br />–Cin>>mark>>grade;<br />–<br />–If(grade= =‘a’)<br />–{<br />–Mark=mark+10;<br />–}<br />–<br />–Cout<<mark;<br />–Getch( );<br />}<br />(ii) if …else statement<br />if..else statement is used to execute one group of statements if the test condition is true or other group if the test condition is false<br />The gentral form<br />If(test condition)<br />{<br />statement block-1;<br />}<br />else<br />{<br />statement block-2;<br />}<br />next statement;<br />when this statement is executed, the computer first evaluates the value of the test condition. If the value is true, statement block-1 is executed and the control is transferred to next statement. If the value is false, statement block-2 is executed and the control is transferred to next statement.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int mark;<br />clrscr();<br />cin>>mark;<br />if(mark=>35)<br />cout<<”passquot;
;<br />else<br />cout<<”fail”;<br />getch();<br />}<br />(iii) Switch statement;<br />Switch case is used for multiple branching.  The switch statement checks the value of an expression against a list of integer or character constants. When match found, the statements associated with that constant are executed.  If no match found the statements under default section are executed.<br />Switch(expression)<br />{<br />case label-1:<br />statement block-1;<br />break;<br />case label-2:<br />statement block-2;<br />break;<br />case label-3:<br />statement block-3;<br />break;<br />……………………………………….<br />………………………………………<br />case label-n:<br />statement block-n;<br />break;<br />default:<br />default statement;<br />break;<br />}<br />next statement<br />ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int day;<br />cout<<”enter  the number is 1 to 7”;<br />cin>>day;<br />switch(day)<br />{<br />case 1:<br />cout<<”Sunday”;<br />break;<br />case 2:<br />cout<<”Monday”;<br />break;<br />case 3:<br />cout<<”Tuesday”;<br />break;<br />case 4:<br />cout<<”Wednesday”;<br />break;<br />case 5:<br />cout<<”Thursday”;<br />break;<br />case 6:<br />cout<<”Friday”;<br />break;<br />case 7:<br />cout<<”Saturday”;<br />break;<br />default:<br />cout<<”Enter the current val”;<br />break;<br />}<br />}<br />Looping Statements:<br />LS are used to execute a group of statements repeatedly unit some condition is satisfied. The looping statements are<br />(i)while statement<br />(ii)do..while statement<br />(iii)for statement<br />While statement:<br />While(test condition)<br />{<br />body of the loop;<br />}<br />next statement;<br />when this statement is executed, the computer first evaluates the test condition. If the value is false, the contron is transferred to next statement. if the value is true then the body of the loop is executed repeatedly until the test condition becomes false. When the test condition becomes false the control is transferred to next statement.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int i=1;<br />while (i<5)<br />{<br />cout<<”god is great”;<br />i++;<br />}<br />}<br />(ii) do…while statement<br />The general form:<br />do<br />{<br />body of the loop;<br />}<br />while(test condition);<br />next statement;<br />when this statement is executed the body of the loop is executed first. Then the test condition is evaluated. If the value is false, the control is transferred to the next statement. If the value is true the body of the loop is executed repeatedly until the test condition becomes false. When the test condition becomes false the control is transferred to the next statement.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int i=1;<br />do<br />{<br />cout<<”god is love”;<br />i++;<br />}<br />while (i<5)<br />}<br />(iv)for statememt:<br />for statement is used to execute a statement or a group of statements repeatedly for a known number of times. The general form is<br />for(initial condition; test condition; increment or decrement)<br />{<br />body of the loop;<br />}<br />next statement;<br />when the for statement is executed the value of the control variable and tested with the test condition. If the value of the test condition is true, the body of the loop will be executed and the control is transferred to the for statement. Then the value of the control variablej is incremented or decremented. When the test condition becomes false the control is transferred to the next statement. The body of the loop is executed repeatedly as long as the test value is true<br />Ex:<br />#include<iostream.h><br />main()<br />{<br />int i,sum=0;<br />for (i=1;i<=50;i++)<br />{<br />sum=sum+i;<br />}<br />cout<<”sum=”<<sum;<br />}<br />FUNCTIONS:<br />Function is defined as a named group of statements. A function can be divided into small modules, then each module can be represented as a function. There are two types of function in c++.<br />1.library function<br />2.user defined functions<br />Library function:<br />LF are functions which are not required to be written by the programmer. But these are available in separate files and the programmer has to include it in the appropriate places.<br />Ex:<br />Cin>>,cout<<,etc.<br />User defined function:<br />A user defined function or simply function has to be written by the programmer to carry out some specific well defined task.<br />All c++ programs consist of one or more function. Out of this one function should be main. This function shows from where the program execution begins.<br />Function definition:<br />The general form is:<br />Function-type function-name ( list of argument)-> header functin<br />{<br />local variable declaration;<br />executable statement;-> statement body<br />return(expression);<br />}<br />A function should be defined before it is used. A function has two parts<br />1.function header<br />2.statement body<br />Rules:<br />1.Function header should not terminate with semicolon. <br />2.list of arguments and argument declaration are optional<br />3.if the function has no list of arguments an empty parentheses is a must<br />Declaring function-type:<br />The function type in the function header is optional. If there is no function type in the function header the return statement by default returns a integer value.<br />But with the help of the option function-type in the function header we can declare the type of the value returned by the function.<br />Ex:<br />1.function-name (list of arguments)<br />                                                    - this function returns integer value<br />2.int function-name (list of arguments)<br />                                                   - this function returns integer value<br />3.float function-name (list of arguments)<br />                                             - this function returns floating point value.<br />Function returning nothing:<br />If the function is not to return any value, we can declare the function of type void, which tells c++ not to save any temporary space for a value to be sent by the function.<br />The general form is:<br />Void function-name ( list of arguments)<br />Inline function:<br />Inline is a keyword prefixed with the function definition. Whenever this function is called the compiler inserts the statements in the function in the calling place.<br />The general form is:<br />Inline data-type function-name(argument list)<br />{<br />function body;<br />}<br />#include<iostream.h><br />#include<conio.h><br />class sample<br />{<br />private:<br />int a,b;<br />public:<br />void getdata()<br />{<br />cout<<”enter the value a and b:”;<br />cin>>a>>b;<br />}<br />void display();<br />};<br />inline void sample::display()<br />{<br />cout<<”The add is”<<a+b<<endl;<br />cout<”The sub is”<<a-b<<endl;<br />}<br />void main()<br />{<br />clrscr();<br />sample a;<br />a.getdata();<br />a.display();<br />getch();<br />}<br />Function Overloading:<br />If a single function performs different operations, then that function is called function overloading. Overloading can be implemented in two ways.<br />1.using different number of arguments<br />2.using different type of arguments<br />Different Numbers:<br />The function with same name can be defined in a same program as given below,<br />1.function with no argument<br />2.function with one argument<br />3.function with two argument<br />………………………………….<br />…………………………………<br />according to our need we have to individually declare and define the functions. When the function is called, depending upon the number of arguments the appropriate function will be executed. This type of implementation is called overloading the function.<br />Different Types:<br />The function with same name can be defined in the same program with same number of arguments with different data types. According to our need we have to individually declare and define the functions. When  the function is called, depending upon the type of the argument the appropriate function will execute. This type of implementation is called overloading the function.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />int add(int a,int b)<br />{<br />return (a+b);<br />}<br />float add(int a, float b)<br />{<br />return(a+b);<br />}<br />float add(float a,float b)<br />{<br />return(a+b);<br />}<br />void main()<br />{<br />clrscr();<br />int a,b;<br />float c,d;<br />cout<<”Enter the int number of a,b”;<br />cin>>a>>b;<br />cout<<”Enter the float number of c,d”;<br />cin>>c>>d;<br />cout<<”The result is”;<br />cout<<add(a,b)<<endl;<br />cout<<add(a,c)<<endl;<br />cout<<add(c,d)<<endl;<br />getch();<br />}<br />STRUCTURES:<br />A structure is defined as a data type to represent different types of data with a single name. The data items in a structure are called members of the structure.<br />Defining a structure:<br />A structure definition contains a keyword struct and a user defined structure-name <br />The general form is:<br />Struct structure-name<br />{<br />data-type member-1;<br />data-type member-2;<br />……………………..<br />……………………<br />data-type member-n;<br />};<br />Ex:<br />Struct student<br />{<br />int number;<br />int age;<br />char sex;<br />};<br />Drawbacks in structures:<br />1.in structures we can keep only data. It is not possible to keep functions.<br />2.data hiding is not possible in structures.<br />Declaration:<br />Structure declaration means defining variables to the already defined structure. We can define variables in two ways.<br />1.variable definition along with the structure definition<br />2.variable definition using structure-name.<br />The general form is:<br />(i) First ways:<br />Struct structure-name<br />{<br />data-type member-1;<br />data-type member-2;<br />……………………..<br />……………………<br />data-type member-n;<br />}variable-1, variable-2,…..variable-n;<br />Ex:<br />Struct student<br />{<br />int number;<br />int age;<br />char sex;<br />}s1,s2,s3;<br />(ii) second ways:<br />Struct structure-name<br />{<br />data-type member-1;<br />data-type member-2;<br />……………………..<br />……………………<br />data-type member-n;<br />};<br />structure-name variable-1, variable-2,…..variable-n;<br />Ex:<br />Struct student<br />{<br />int number;<br />int age;<br />char sex;<br />};<br />student s1,s2,s3;<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />struct employee<br />{<br />char sex;<br />int number;<br />float salary;<br />}dept1;<br />cout<<”Give the values to the numbers”;<br />cout<<”Give the sex”;<br />cin>>dept1.sex;<br />cout<<”Give the number”;<br />cin>>dept1.number;<br />cout<<”Give the salary”;<br />cin>>dept1.salary;<br />cout<<”Employee-details”;<br />cout<<”Sex:”<<dept1.sex;<br />cout<<”Number:”<<dept1.number;<br />cout<<”Salary:”<<dept1.salary;<br />}<br />CLASS:<br />A class is a userdefined datatype. It contains data and its related functions. The data and functions in a class can be defined by any one of the following visibility modifiers.<br />1.public<br />2.private<br />3.protected<br />public:<br />if a data or function defined as public, it can be accessed from outside the class.<br />Private:<br />If a data or function is defined as private, it cannot be accessed from outside the class. This concept is called data hiding.<br />Protected:<br />If a data or function is defined as protected, it can be accessed only by the classes derived from this class.<br />Class definition:<br />A class definition contains a keyword class and a user defined class-name followed by data and functions named as members of the class. The members are enclosed with in braces.<br />Syntax:<br />Class class-name<br />{<br />private:<br />variable declaration;<br />function declaration or definition;<br />public:<br />variable declaration;<br />function declaration or definition;<br />protected:<br />variable declaration;<br />function declaration or definition;<br />};<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class fact<br />{<br />private:<br />int I,n,sum;<br />public:<br />void getdata()<br />{<br />cout<<”Enter the value n:”;<br />cin>>n;<br />}<br />void display()<br />{<br />I=1;<br />Sum=1;<br />While(I<=n)<br />{<br />sum=sum+I;<br />I++;<br />}<br />cout<<”Fact is”<<sum;<br />}<br />};<br />void main()<br />{<br />fact f;<br />clrscr();<br />f.getdata();<br />f.display();<br />getch();<br />}<br />Defining Member Function:<br />Member functions can be defined in two places:<br />i.Outside the class definition.<br />ii.Inside the class definition<br />Outside the class definition:<br />Member functions that are declared inside a class have to be defined separately outside the class. Their definitions are very much like the normal functions. An important difference between a member function and a normal function is that a member function incorporates a membership ‘identity label’ in the header. This ‘label’ tells the compiler which class the function belongs to.<br />The general form is:<br />Return-type class-name :: function-name (argument declaration)<br />{<br />function body;<br />}<br />Ex:<br />Void item :: getdata(int a, float b)<br />{<br />number = a;<br />cost = b;<br />}<br />Inside the class definition:<br />Another method of defining a member function is to replace the function declaration by the actual function definition inside the class.<br />Ex:<br />Class item<br />{<br />int no;<br />float cost;<br />public:<br />void getdata(int a,float b);<br />void putdata(void)<br />{<br />cout<<no<<””;<br />cout<<cost<<””;<br />}<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class item<br />{<br />int number;<br />float cost;<br />public:<br />void getdata(int a,float b);<br />void putdata(void)<br />{<br />cout<<”Number:”<<number<<””;<br />cout<<”Cost:”<<cost<<””;<br />}<br />};<br />void item :: getdata(int a, float b)<br />{<br />number =a;<br />cost =b;<br />}<br />int main( )<br />{<br />item x;<br />cout<<” object x”<<””;<br />x.getdata(100,299.95);<br />x.putdata( );<br />item y;<br />cout<<” object y”<<””;<br />y.getdata(200, 175.50);<br />y.putdata();<br />return 0;<br />}<br />Array within a Class:<br />The arrays can be used as member variables in a class. The following class definition is valid<br />Class array<br />{<br />int a[size];<br />public:<br />void setval(void);<br />void display(void);<br />};<br />the array variable a[] declared as a private member of the class array can be used in the member functions, like any other array variable. We can perform any operations on it. For instance, in the above class definition, the member function setval() sets the values of elements of the array a[], and display() function display the values.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class array<br />{<br />private:<br />int a[10];<br />int size;<br />public:<br />void read();<br />int sum();<br />};<br />void array :: read()<br />{<br />int I;<br />cout<<” enter the size:”;<br />cin>>size;<br />cout<<” enter the element one by one:”;<br />for(I=0;I<size;I++)<br />cin>>a[I];<br />}<br />int array :: sum()<br />{<br />int s=0,I;<br />for(I=0;I<size;I++)<br />s += a[I];<br />return s;<br />}<br />void main()<br />{<br />int sum;<br />array b;<br />b.read();<br />b.display();<br />sum=b.sum();<br />cout<<” sum=”<<sum;<br />}<br />Array of Objects:<br />With the help of arrays, more than one object in a class in a class can be defined by a single name. This is called array of objects.<br />The general form is:<br />Class-name array-name [size];<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class employee<br />{<br />char name[10];<br />float age;<br />public:<br />void getdata(void);<br />void putdata(void);<br />};<br />void employee :: getdata (void)<br />{<br />cout<<”Enter name:”;<br />cin>>name;<br />cout<<”Enter age:”;<br />cin>>age;<br />}<br />void employee :: putdata (void)<br />{<br />cout<<”Name:”<<name<<””;<br />cout<<”Age:”<<age<<””;<br />}<br />const int size=3;<br />int main()<br />{<br />employee manager[size];<br />for(int I=0;I<size;I++)<br />{<br />cout<<” Details of manager”<<I+1<<””;<br />manager[I].getdata();<br />}<br />cout<<””;<br />for(I=0;I<size;I++)<br />{<br />cout<<” manager”<<I+1<<””;<br />manager[I].putdata();<br />}<br />return 0;<br />}<br />Friend Function:<br />In a class, private member variables can be accessed only by the member functions in that class. But with the help of friend function we can access the private member variables in a class. The friend function should be a non-member class<br />Thus friendly functions are non-member function which are used to access the private member variable in a class.<br />The general form is:<br />Class class-name<br />{<br />private:<br />……….<br />………<br />public:<br />………<br />……….<br />Friend return-type non-member function name(argument list);<br />};<br />Rules:<br />1.The keyword friend can be used only inside the class.<br />2.More than one friend function can be declared in a class.<br />3.A function can be friend to more than one class.]<br />4.The friend function definition should not contain the scope operator.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class sample<br />{<br />int a;<br />int b;<br />public:<br />void setvalue()<br />{<br />a=25;<br />b=40;<br />}<br />friend float mean(sample s);<br />};<br />float mean(sample s)<br />{<br />return float(s.a+s.b)/2.0;<br />}<br />int main()<br />{<br />sample x;<br />x.setvalue();<br />cout<<”Mean value=”<<mean(x)<<””;<br />return 0;<br />}<br />Constructors<br />Constructor is a special member function. This is used to give initial values to member variables. The constructor is invoked whenever an object of its associated class is created.<br />The general form is:<br />Constructor name (parameters)<br />{<br />to give initial values to member variables;<br />}<br />Rules:<br />1.It should be declared as public<br />2.No return type needed.<br />3.When the object is declared for the class, it automatically executes the constructor and initialize the variables.<br />There are 3 types of constructors.<br />(i)Constructors without parameters<br />(ii)Constructors with paramerters<br />(iii)Multiple constructors.<br />Constructors without parameters:<br />If a constructor has no arguments then it is called constructor without paramerter.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class abc<br />{<br />int i;<br />public:<br />abc()<br />{<br />i=10;<br />clrscr();<br />}<br />void print()<br />{<br />cout<<quot;
 value=quot;
<<i;<br />}<br />void incre()<br />{<br />i++;<br />}<br />};<br />void main()<br />{<br />abc c1,c2;<br />c1.print();<br />c2.print();<br />c1.incre();<br />c1.incre();<br />c2.incre();<br />c1.print();<br />c2.print();<br />getch();<br />}<br />Constructors with parameters:<br />If a constructor has arguments then it is called constsructor with parameters. The value of the arguments should be given along the object declaration.<br />The general form is<br />Class-name object ( argument-value-list);<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class item<br />{<br />private:<br />int item_code;<br />float item_price;<br />public:<br />item()<br />{<br />item_code=0;<br />item_price=0.0;<br />}<br />item(int x, float y)<br />{<br />item_code=x;<br />item_price=y;<br />clrscr();<br />}<br />void get_data()<br />{<br />cout<<quot;
 enter the code:quot;
;<br />cin>>item_code;<br />cout<<quot;
 enter the price:quot;
;<br />cin>>item_price;<br />}<br />void disp_data()<br />{<br />cout<<quot;
 item code=quot;
<<item_code;<br />cout<<quot;
 itemprice=quot;
<<item_price;<br />}<br />};<br />void main()<br />{<br />item obj1;<br />item obj2(2,10.50);<br />obj1.disp_data();<br />obj2.disp_data();<br />obj1.get_data();<br />obj1.disp_data();<br />getch();<br />}<br />Multiple constructors:<br />If a class has more than one constructor, then it is called multiple constructors. This technique is called overloading constructors.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class con<br />{<br />int a,b,c;<br />public:<br />con(int x);<br />con(int x, int y);<br />con(int x, int y, int z);<br />};<br />con :: con(int x)<br />{<br />a = x;<br />cout<<quot;
Value of A is:quot;
<<a<<endl;<br />}<br />con :: con(int x, int y)<br />{<br />a = x;<br />b = y;<br />cout<<quot;
Value of A is:quot;
<<a<<endl;<br />cout<<quot;
Value of B is:quot;
<<b<<endl;<br />}<br />con :: con(int x, int y, int z)<br />{<br />a = x;<br />b = y;<br />c = z;<br />cout<<quot;
Value of A is:quot;
<<a<<endl;<br />cout<<quot;
Value of B is:quot;
<<b<<endl;<br />cout<<quot;
Value of C is:quot;
<<c<<endl;<br />}<br />void main()<br />{<br />clrscr();<br />cout<<quot;
 Creating Object With One Valuequot;
<<endl;<br />con one(10);<br />cout<<quot;
 Creating Object With Two Valuequot;
<<endl;<br />con two(100,200);<br />cout<<quot;
 Creating Object With Three Valuequot;
<<endl;<br />con three(1000,2000,3000);<br />getch();<br />}<br />Constructor with default argument:<br />If we give initial values to the member variables inside the argument list of constructor then it is called constructor with default arguments.<br />The general form is:<br />Constructor-name ( argument-type argument-variable = value1, …);<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />#include<string.h><br />class abc<br />{<br />int x;<br />float y;<br />char z[5];<br />public:<br />abc(int x1=10, float y1=20.0, char z1[10]=quot;
Raamquot;
);<br />void read()<br />{<br />cout<<quot;
 Enter x,y,z:quot;
;<br />cin>>x>>y>>z;<br />}<br />void print()<br />{<br />cout<<quot;
 valuej of x=quot;
<<x;<br />cout<<quot;
 valuej of y=quot;
<<y;<br />cout<<quot;
 valuej of z=quot;
<<z;<br />}<br />};<br />abc :: abc(int x1, float y1, char z1[])<br />{<br />x=x1;<br />y=y1;<br />strcpy(z,z1);<br />clrscr();<br />}<br />void main()<br />{<br />abc a1;<br />abc a2(5);<br />abc a3(100, 12.30);<br />abc a4(20, 16.39, quot;
Rajaquot;
);<br />a1.print();<br />a2.print();<br />a3.print();<br />a4.print();<br />a4.read();<br />a4.print();<br />getch();<br />}<br />DYNAMIC INITIALISATIONJOF OBJEECTS:<br />Dynamic initialization of objects means giving initial values to the member variables during run time. With the help of this facility, we can give different type of values to the member variables depending upon the situation. <br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />int no;<br />float weight;<br />int age;<br />public:<br />student()<br />{<br />no=0;<br />weight=0.0;<br />age=0;<br />}<br />student(int n1,float w1,int a1);<br />student(int n2,int w2,float a2);<br />void print()<br />{<br />cout<<quot;
 Number=quot;
<<no;<br />cout<<quot;
 weight=quot;
<<weight;<br />cout<<quot;
 age=quot;
<<age;<br />}<br />};<br />student :: student (int n1,float w1,int a1)<br />{<br />no=n1;<br />weight=w1;<br />age=a1;<br />}<br />student :: student(int n2,int w2, float a2)<br />{<br />no=n2;<br />weight=w2;<br />age=a2;<br />}<br />void main()<br />{<br />student s1,s2;<br />int n,a,w1;<br />float w,a1;<br />clrscr();<br />s1.print();<br />cout<<quot;
 enter number, weight, age:quot;
;<br />cin>>n>>w>>a;<br />s1=student(n,w,a);<br />s1.print();<br />cout<<quot;
 enter thenumber,weight,age:quot;
;<br />cin>>n>>w1>>a1;<br />s2=student(n,w1,a1);<br />s2.print();<br />getch();<br />}<br />COPY CONSTRUCTORS:<br />Copy constructors are used to declare and initialize an object with the values from another object. A copy constructor takes a reference to an object of the same class as itself as an argument. <br />Declaration:<br />Constructor-name ( class-name &object-name )<br />    {<br />         ……………….<br />         ……………….<br />   }<br />Copy constructor calling:<br />Class-name new-obj-name ( old-obj-name );<br />Or<br />Class-name new-obj-name = old-obj-name;<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />int no;<br />int mark;<br />public:<br />student (int n1,int m1)<br />{<br />no = n1;<br />mark = m1;<br />}<br />student (student &a1)<br />{<br />no = a1.no;<br />mark = a1.mark;<br />}<br />void print()<br />{<br />cout<<quot;
 No=quot;
<<no;<br />cout<<quot;
 Mark=quot;
<<mark;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />student s1(20,400);<br />student s2=s1;<br />student s3(10,500);<br />student s4(s3);<br />s1.print();<br />s2.print();<br />s3.print();<br />s4.print();<br />getch();<br />}<br />DYNAMIC CONSTRUCTORS:<br />If the memory space for the constructor (memory variables) is allocated during the constructor calling, then it is called dynamic constructor. This is done with the help of operator new. Depending on the size of the values in the variable the memory occupation of the objects varies.<br />Examples:<br />#include<iostream.h><br />#include<conio.h><br />#include<string.h><br />class string<br />{<br />char *name;<br />int length;<br />public:<br />string()<br />{<br />length=0;<br />name = new char [length + 1];<br />}<br />string (char *s)<br />{<br />length=strlen(s);<br />name = new char [length +1];<br />strcpy (name,s);<br />}<br />void display(void)<br />{<br />cout<<name<<quot;
quot;
;<br />}<br />void join(string &a, string &b);<br />};<br />void string :: join(string &a, string &b)<br />{<br />length = a.length + b.length;<br />delete name;<br />name = new char [length + 1];<br />strcpy(name,a.name);<br />strcat(name,b.name);<br />};<br />int main()<br />{<br />char *first = quot;
josephquot;
;<br />clrscr();<br />string name1(first), name2(quot;
louisquot;
),name3(quot;
lagrangequot;
),s1,s2;<br />s1.join(name1, name2);<br />s2.join(s1, name3);<br />name1.display();<br />name2.display();<br />name3.display();<br />s1.display();<br />s2.display();<br />getch();<br />return 0;<br />}<br />Destructors:<br />Destructor is a member function used to deallocate the memory space allocated by the constructor. A destructor is a function that automatically executes when an object is destroyed. A destructor function gets executed whenever an instance of the class to which it belongs goes out of existence. <br />The general form is<br />~ destructor-name()<br />{<br />   …………… <br />             ……………<br />}<br />~ ( tilde)-> destructor operator<br />destructor-name-> name of the class<br />The complier automatically deallocates the memory space when the program ends.<br />Rules:<br />1.It won’t take any arguments<br />2.It won’t return any value<br />3.It cannot be declared static, const<br />4.It should have public access in the class declaration.<br />5.If new operator is used inside the constructor for allocating memory, delete operator should be used inside the destructor to free the memory<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class baseA<br />{<br />public:<br />baseA()<br />{<br />cout<<”base class constructor”;<br />}<br />~baseA()<br />{<br />cout<<”base class destructor”;<br />}<br />};<br />class derivedD :public baseA<br />{<br />derivedD()<br />{<br />cout<<”derived class constructor”;<br />}<br />~derivedD()<br />{<br />cout<<”derived class destructor”;<br />}<br />};<br />void main()<br />{<br />derivedD obj;<br />}<br />Operator Overloading:<br />Operator Overloading is a technique used to change the predefined operation of an operator for existing data types to a new operation for user defined data types.<br />All the operators can be overloaded except the operators given below.<br />1.Conditional operator ?:<br />2.Scope resolution operator ::<br />3.Class member operator .*<br />4.Size of operator sizeof<br />5.Membership operator .<br />Defining operator overloading:<br />Overloading can be defined inside the class or outside the class. If defined inside, the declaration and definition are same. If defined outside, it should be declared inside using prototype.<br />The general form is:<br />Defining inside the class:<br />Return-type operator symbol ( argument list )<br />{<br />...............<br />...............<br />}<br />Defining outside the class:<br />Declaration inside the class:<br />Return-type operator symbol ( list of argument type);<br />Definition outside the class<br />Return-type class-name :: operator symbol ( list of argument type)<br />{<br />................<br />................<br />}<br />Rules:<br />1.Operator overloading function should be a public member function or friend function.<br />2.Unary ++, -- can be called by prefix or post fix form.<br />Unary Operator Loading:<br />If the symbol used in a operator overloading function is a unary operator (-, +, ++, --, etc) then this overloading is called unary operator overloading.<br />Let us consider the unary minus operator. A minus operator when used as a unary, takes just one operand. We know that this operator changes the sign of an operand when applied to a basic data item. <br />The general form is:<br />Return-type operator unary-symbol ( )<br />{<br />...............<br />}<br />Calling the function:<br />Symbol object-name;<br />When this statement is executed, the symbol calls the overload function and the object value is changed depending jupon the value of the overload function.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class space<br />{<br />int x,y,z;<br />public:<br />void getdata(int a, int b, int c);<br />void display(void);<br />void operator-();<br />};<br />void space :: getdata(int a, int b, int c)<br />{<br />x=a;<br />y=b;<br />z=c;<br />}<br />void space :: display(void)<br />{<br />cout<<x<<quot;
 quot;
;<br />cout<<y<<quot;
 quot;
;<br />cout<<z<<quot;
quot;
;<br />}<br />void space :: operator - ()<br />{<br />x=-x;<br />y=-y;<br />z=-z;<br />}<br />int main()<br />{<br />space s;<br />clrscr();<br />s.getdata(10,-20,30);<br />cout<<quot;
s:quot;
;<br />s.display();<br />-s;<br />cout<<quot;
s:quot;
;<br />s.display();<br />getch();<br />return 0;<br />}<br />Binary Operator Loading:<br />If the symbol used in a operator overloading function is a binary operator (+,-,*,/,+=,-=,etc) then this overloading is called binary operator overloading. This takes only one argument from argument list and the other from the calling object.<br />The general form is:<br />Return-type operator binary-symbol (data-type1)<br />{<br />..............<br />}<br />Calling the overloaded function:<br />The general form is:<br />Object3 = object1 binary-symbol object2<br />When this statement is executed object1 calls the overloading symbol and passes the object2 as a argument to the overload function. Finally the calculated value will be assigned to object3 in the left hand side.<br />Rules:<br />1.It requires one argument explicitly.<br />2.The symbol is the overload function and calling must be same.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class complex<br />{<br />float x,y;<br />public:<br />complex()<br />{<br />}<br />complex(float real, float img)<br />{<br />x=real;<br />y=img;<br />}<br />complex operator + (complex c);<br />void display(void);<br />};<br />complex complex :: operator + (complex c)<br />{<br />complex temp;<br />temp.x = x+c.x;<br />temp.y = y+c.y;<br />return(temp);<br />}<br />void complex :: display(void)<br />{<br />cout<<x<<quot;
+jquot;
<<y<<quot;
quot;
;<br />}<br />int main()<br />{<br />complex c1,c2,c3;<br />clrscr();<br />c1 = complex(2.5,3.5);<br />c2 = complex(1.6,2.7);<br />c3 = c1 + c2;<br />cout<<quot;
c1=quot;
;<br />c1.display();<br />cout<<quot;
c2=quot;
;<br />c2.display();<br />cout<<endl;<br />cout<<quot;
c3=quot;
;<br />c3.display();<br />getch();<br />return 0;<br />}<br />Manipulation of Strings Using Operators:<br />In c++ there are no operators available for string manipulation. Butwe can overload the operators for doing the required string operation (concatination, comparision, ect.,)<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />#include<string.h><br />class string1<br />{<br />private:<br />char str[40];<br />public:<br />       void getstr()<br />       {<br />cout<<quot;
Enter a string:quot;
;<br />cin>>str;<br />}<br />  string1 operator + (string1 str1)<br />  {<br />  string1 str3;<br />  strcpy(str3.str,(strcat(str,str1.str)));<br />  return(str3);<br />  }<br />  void printstr()<br />  {<br />  cout<<str;<br />  }<br />};<br />void main()<br />{<br />string1 a,b,c;<br />clrscr();<br />a.getstr();<br />b.getstr();<br />cout<<quot;
The first string is:quot;
;<br />a.printstr();<br />cout<<quot;
The 2nd string is:quot;
;<br />b.printstr();<br />cout<<quot;
The concatenation string is:quot;
;<br />c = a + b;<br />c.printstr();<br />getch();<br />}<br />Rules for Overloading Operators:<br />1.Only existing operators can be overloaded. New operators cannot be created.<br />2.We cannot change the basic meaning of an operator. That is, we cannot redefine the ( * ) operator to  add one value to another.<br />3.We cannot overload all operators. The operators which we cannot overload are sizeof, .*, ::, ., ?:<br />4.Symbol used in the overload function andjin the function call must be unique.<br />5.Binary operator overloading using member function requires one argument explicitly. But using friend function requires two arguments explicitly.<br />6.We cannot overload all operators using friend function. <br />They are =, ( ), [ ], -><br />7.The syntax rule for original operator and overloaded operator are same.<br />INHERITANCE:<br />Inheritance is the process of creating new classes from the existing classes. the new classes are called derived classes. the existing classes are called base classes.<br />The derived classes inherit all properties of the base classes plus its own properties. the process of inheritance does not affect the base classes.<br />Advantages:<br />1. New classes can be derived by the user from the existing classes without any modification.<br />2. It saves time and money.<br />3. It reduces program coding time<br />4. It increases the reliability of the program.<br />Syntax:<br />class derived_class_name : mode base_class_name<br />{<br />       new member variable declaraction;<br />       ...................................<br />       ....................................<br />       new member function declaracion and definition<br />       ...................................<br />       ...................................<br />};<br />SINGLE INHERITANCE:<br />If a derived class is derived from a single base class, then it is called single inheritance. single inheritance can be derived publicly or privately.<br />If publicly derived, the public and protected member functions of the base class becomes public and protected in the derived class. if privately derived the public and protected member functions of the base class becomes private in the derived class.<br />Syntax:<br />class base<br />{<br />..............<br />..............<br />};<br />class derive 1 : mode base<br />{<br />..............<br />..............<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />private:<br />int reg_no;<br />protected:<br />char name[20];<br />public:<br />void read()<br />{<br />cout<<quot;
 Enter the reg_no:quot;
;<br />cin>>reg_no;<br />cout<<quot;
 Enter the name:quot;
;<br />cin>>name;<br />}<br />void print()<br />{<br />cout<<quot;
 Reg_no=quot;
<<reg_no;<br />}<br />};<br />class computer:public student<br />{<br />private:<br />int roll_no;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot;
 Enter the roll_no:quot;
;<br />cin>>roll_no;<br />}<br />void print1()<br />{<br />cout<<quot;
 roll_no=quot;
<<roll_no;<br />cout<<quot;
 Name=quot;
<<name;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />computer s1;<br />s1.read1();<br />s1.print();<br />s1.print1();<br />getch();<br />}<br />MULTILEVEL INHERITANCE:<br />Multilevel inheritance is a process to derive classes in a sequential order. A base class is treated as level 1 class. from this class we can derive a new class and is treated as level 2 class. from the level 2 class we can derive another new class and is treated as level 3 class and so on.<br />The lower level class inherits the higher level classes along the inheritance path.<br />Syntax:<br />class base<br />{<br />..............<br />..............<br />};<br />class derive 1 : mode base<br />{<br />..............<br />..............<br />};<br />class derive 2 : mode derive 1<br />{<br />...........<br />...........<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[12];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot;
 Enter the number and model:quot;
;<br />cin>>reg_no>>model;<br />}<br />void print()<br />{<br />cout<<quot;
 Reg_no=quot;
<<reg_no;<br />cout<<quot;
 Model=quot;
<<model;<br />}<br />};<br />class two_wheeler : public vehicle<br />{<br />protected:<br />int no_gear;<br />int power;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot;
 Enter the number of gear,power:quot;
;<br />cin>>no_gear>>power;<br />}<br />void print1()<br />{<br />print();<br />cout<<quot;
 NO_gear=quot;
<<no_gear;<br />cout<<quot;
 power=quot;
<<power;<br />}<br />};<br />class scotter:public two_wheeler<br />{<br />protected:<br />char manufacturer[20];<br />char owner[20];<br />public:<br />void read2()<br />{<br />read1();<br />cout<<quot;
 Enter the name of manufacturer and owner:quot;
;<br />cin>>manufacturer>>owner;<br />}<br />void print2()<br />{<br />print1();<br />cout<<quot;
 Manufacturer name=quot;
<<manufacturer;<br />cout<<quot;
 owner name=quot;
<<owner;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />scotter s1;<br />s1.read2();<br />s1.print2();<br />getch();<br />} <br />MULTIPLE INHERITANCE:<br />A class derived from more than one base classes is called multiple inheritance.<br />Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. it is like a child inheriting the physical features of one parent and the intelligence of another.<br />Syntax:<br />class base 1<br />{<br />................<br />................<br />};<br />class base 2<br />{<br />................<br />................<br />};<br />class base 3<br />{<br />................<br />................<br />};<br />class derived : mode base 1, mode base 2, mode base 3<br />{<br />................<br />................<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class income<br />{<br />protected:<br />float amount;<br />public:<br />void read()<br />{<br />cout<<quot;
 give the amount:quot;
;<br />cin>>iamount;<br />}<br />};<br />class expenditure<br />{<br />protected:<br />float eamount;<br />public:<br />void read1()<br />{<br />cout<<quot;
 give the amount:quot;
;<br />cin>>eamount;<br />}<br />};<br />class n_income:public income, public expenditure<br />{<br />private:<br />float namount;<br />public:<br />void print()<br />{<br />cout<<quot;
 income=quot;
<<iamount;<br />cout<<quot;
 expenditure=quot;
<<eamount;<br />cout<<quot;
 net amount=quot;
<<namount;<br />}<br />void read2()<br />{<br />read();<br />read1();<br />namount=iamount-eamount;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />n_income n1;<br />n1.read2();<br />n1.print();<br />getch();<br />}<br />HIERARCHY INHERITANCE:<br />Hierarchy inheritance is a process to derive classes in a hierarchical order. A base class is treated as level 1 class. From this class we can derive new classes and is treated as level 2 classes. from a level 2 class we can derive a another new classes and is treated as level 3 and so on.<br />The lower level classes inherits the higher level classes along inheritance path.<br />Syntax:<br />class base<br />{<br />..............<br />..............<br />};<br />class derive 1 : mode base<br />{<br />..............<br />..............<br />};<br />class derive 2 : mode base<br />{<br />...........<br />...........<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[10];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot;
 Enter the reg_no, and model:quot;
;<br />cin>>reg_no>>model;<br />}<br />void print()<br />{<br />cout<<quot;
 Reg_no=quot;
<<reg_no;<br />cout<<quot;
 Model=quot;
<<model;<br />}<br />};<br />class two_wheeler : public vehicle<br />{<br />protected:<br />int no_gear;<br />int power;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot;
 Enter the number of gear and power:quot;
;<br />cin>>no_gear>>power;<br />}<br />void print1()<br />{<br />print();<br />cout<<quot;
 No_gear=quot;
<<no_gear;<br />cout<<quot;
 Power=quot;
<<power;<br />}<br />};<br />class scooter : public two_wheeler<br />{<br />protected:<br />char manufacturer[20];<br />char owner[20];<br />public:<br />void read2()<br />{<br />read1();<br />cout<<quot;
 Enter the name of manufacturer and owner:quot;
;<br />cin>>manufacturer>>owner;<br />}<br />void print2()<br />{<br />print1();<br />cout<<quot;
 Manufacturer:quot;
<<manufacturer;<br />cout<<quot;
 Owner:quot;
<<owner;<br />}<br />};<br />class four_wheeler : public vehicle<br />{<br />protected:<br />char fuel[20];<br />int no_cylinder;<br />public:<br />void read3()<br />{<br />read();<br />cout<<quot;
 give the type of fuel and no.of cylinder:quot;
;<br />cin>>fuel>>no_cylinder;<br />}<br />void print3()<br />{<br />print();<br />cout<<quot;
 fuel type=quot;
<<fuel;<br />cout<<quot;
 number of cylinder=quot;
<<no_cylinder;<br />}<br />};<br />class car : public four_wheeler<br />{<br />protected:<br />char name[20];<br />char owner[10];<br />public:<br />void read4()<br />{<br />read3();<br />cout<<quot;
 enter the name and owner:quot;
;<br />cin>>name>>owner;<br />}<br />void print4()<br />{<br />print3();<br />cout<<quot;
 name=quot;
<<name;<br />cout<<quot;
 owner=quot;
<<owner;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />scooter s1;<br />car c1;<br />s1.read2();<br />s1.print2();<br />c1.read4();<br />c1.print4();<br />getch();<br />}<br />HIBRID INHERITANCE:<br />An inheritance class derived from a base class and an inheritance class or two inheritance classes is called hybrid inheritance.<br />-  Ex: (Multilevel, Multiple Inheritance):<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[10];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot;
 Enter the reg_no and model:quot;
;<br />cin>>reg_no>>model;<br />}<br />};<br />class four_wheeler : public vehicle<br />{<br />protected:<br />int no_cylinder;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot;
 enter the no. of cylinder:quot;
;<br />cin>>no_cylinder;<br />}<br />};<br />class car : public four_wheeler<br />{<br />protected:<br />char name[10];<br />char owner[20];<br />int no_seats;<br />float price1;<br />public:<br />void read2()<br />{<br />read1();<br />cout<<quot;
 enter the name, owner, no_seats, prince1:quot;
;<br />cin>>name>>owner>>no_seats>>price1;<br />}<br />};<br />class ac<br />{<br />protected:<br />float capacity;<br />char make[20];<br />float price2;<br />public:<br />void read3()<br />{<br />cout<<quot;
 Enter capacity, make, price2:quot;
;<br />cin>>capacity>>make>>price2;<br />}<br />};<br />class ac_car:public car, public ac<br />{<br />protected:<br />float total_price;<br />float fitting_charge;<br />public:<br />ac_car()<br />{<br />fitting_charge = 1000.0;<br />}<br />void read4()<br />{<br />read2();<br />read3();<br />total_price=price1+price2;<br />}<br />void print()<br />{<br />cout<<quot;
 Reg_no=quot;
<<reg_no;<br />cout<<quot;
 Model=quot;
<<model;<br />cout<<quot;
 No_cylinder=quot;
<<no_cylinder;<br />cout<<quot;
 Name=quot;
<<name;<br />cout<<quot;
 Owner=quot;
<<owner;<br />cout<<quot;
 NO_Seats=quot;
<<no_seats;<br />cout<<quot;
 Total_price=quot;
<<total_price+fitting_charge;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />ac_car a1;<br />a1.read4();<br />a1.print();<br />getch();<br />}<br />AMBIGUITY:<br />Whenever a data member and member function are defined with the same name in both the base and the derived classes, these names must be without ambiguity. The scope resolution operator ( :: ) may be used to refer to any base member explicitly. This allows access to a name that has been redefined in the derived class.<br />Two base classes have functions with the same name, and also the same function name in derived class, we have ambiguity. The compiler which function is accessed from where.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class a<br />{<br />public:<br />void display()<br />{<br />cout<<quot;
 I am in class Aquot;
;<br />}<br />};<br />class b<br />{<br />public:<br />void display()<br />{<br />cout<<quot;
 I am in class Bquot;
;<br />}<br />};<br />class c:public a,public b<br />{<br />};<br />void main()<br />{<br />clrscr();<br />c obj;<br />obj.display(); // ambiguity compilation error<br />obj.a::display();<br />obj.b::display();<br />getch();<br />}<br />VIRTUAL BASE CLASS<br />When a class is made a virtual base class, c++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exist between the virtual base class and a derived class<br />The 'child' has two direct base classes 'parent1' and 'parent2' which themselvces have a common base clas 'grandparent'. the 'child' inherits the gtraits of 'grandparent' via two separate paths. it can also inherit directly as shown by the broken line. the 'grandparent' is sometimes referred to as indirect base class.<br />Syntax:<br />class a<br />{<br />.............<br />.............<br />};<br />class b1 : virtual public a<br />{<br />...............<br />...............<br />};<br />class b2 : virtual public a<br />{<br />..............<br />..............<br />};<br />class c : public b1, public b2<br />{<br />...............<br />..........<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[10];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot;
 Enter the REG_NO and MODEL:quot;
;<br />cin>>reg_no>>model;<br />}<br />};<br />class road_vehicle : virtual public vehicle<br />{<br />protected:<br />int no_wheel;<br />char fuel[10];<br />public:<br />void read1()<br />{<br />cout<<quot;
 Enter the WHEELS and FUEL:quot;
;<br />cin>>no_wheel>>fuel;<br />}<br />};<br />class water_vehicle : virtual public vehicle<br />{<br />protected:<br />int no_leaf;<br />char fuel1[10];<br />public:<br />void read2()<br />{<br />cout<<quot;
 Enter the NO_OF LEAF and FUEL:quot;
;<br />cin>>no_leaf>>fuel1;<br />}<br />};<br />class road_water_vehicle : public road_vehicle,public water_vehicle<br />{<br />private:<br />int status;<br />public:<br />void read3()<br />{<br />read();<br />read1();<br />read2();<br />cout<<quot;
 Enter the Status: 1 or Another No:quot;
;<br />cin>>status;<br />}<br />void print()<br />{<br />if(status==1)<br />{<br />cout<<quot;
Reg_no=quot;
<<reg_no;<br />cout<<quot;
Model=quot;
<<model;<br />cout<<quot;
No_wheel=quot;
<<no_wheel;<br />cout<<quot;
Fuel=quot;
<<fuel;<br />}<br />else<br />{<br />cout<<quot;
Reg_no=quot;
<<reg_no;<br />cout<<quot;
Model=quot;
<<model;<br />cout<<quot;
No_leaf=quot;
<<no_leaf;<br />cout<<quot;
Fuel=quot;
<<fuel1;<br />}<br />}<br />};<br />void main()<br />{<br />clrscr();<br />road_water_vehicle rw;<br />rw.read3();<br />rw.print();<br />getch();<br />}<br />ABSTRACT CLASSES:<br />An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class (to be inherited by other classes). It is  a design concept in program development and provides a base upon which other classes may be built.<br />A class is said to be abstrat class if it satisfies the following conditions.<br />1. It should act as base class.<br />2. It should not be used to create any objects.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />private:<br />int reg_no;<br />protected:<br />char name[20];<br />public:<br />void read()<br />{<br />cout<<quot;
 Enter the reg_no:quot;
;<br />cin>>reg_no;<br />cout<<quot;
 Enter the name:quot;
;<br />cin>>name;<br />}<br />void print()<br />{<br />cout<<quot;
 Reg_no=quot;
<<reg_no;<br />}<br />};<br />class computer:public student<br />{<br />private:<br />int roll_no;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot;
 Enter the roll_no:quot;
;<br />cin>>roll_no;<br />}<br />void print1()<br />{<br />cout<<quot;
 roll_no=quot;
<<roll_no;<br />cout<<quot;
 Name=quot;
<<name;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />computer s1;<br />s1.read1();<br />s1.print();<br />s1.print1();<br />getch();<br />}<br />NESTING OF CLASSES ( CONTAINER CLASSES )<br />class student<br />{<br />private:<br />char school_name[20];<br />char degree[20];<br />public:<br />void get_student_detail()<br />{<br />cout<<quot;
Enter the name of school:quot;
;<br />cin>>school_name;<br />cout<<quot;
Enter the higher degree earned quot;
;<br />cout<<quot;
(Bachelor, Master, ph.d, Highschool ect.,)quot;
;<br />cin>>degree;<br />}<br />void print_detail()<br />{<br />cout<<quot;
 School or University:quot;
<<school_name;<br />cout<<quot;
 Higher degree earned:quot;
<<degree;<br />}<br />};<br />class employee<br />{<br />private:<br />char name[80];<br />int empno;<br />public:<br />void get_emp_detail()<br />{<br />cout<<quot;
Enter Emoloyee No:quot;
;<br />cin>>empno;<br />cout<<quot;
Enter employee name:quot;
;<br />cin>>name;<br />}<br />void print_emp_detail()<br />{<br />cout<<quot;
 Employee number:quot;
<<empno<<quot;
quot;
;<br />cout<<quot;
 Employee name:quot;
<<name<<quot;
quot;
;<br />}<br />};<br />class administrative<br />{<br />private:<br />char designation[80];<br />float basic;<br />employee emp;<br />student stud;<br />public:<br />void get_data()<br />{<br />emp.get_emp_detail();<br />cout<<quot;
Enter designation:quot;
;<br />cin>>designation;<br />cout<<quot;
Enter the basic pay:quot;
;<br />cin>>basic;<br />stud.get_student_detail();<br />}<br />void print()<br />{<br />emp.print_emp_detail();<br />cout<<quot;
Designation:quot;
<<designation<<quot;
quot;
;<br />cout<<quot;
Basic pay:quot;
<<basic<<quot;
quot;
;<br />stud.print_detail();<br />}<br />};<br />void main()<br />{<br />administrative a;<br />clrscr();<br />cout<<quot;
Enter the details of administrative staffquot;
;<br />a.get_data();<br />cout<<quot;
The detailsj of the staff are:quot;
;<br />a.print();<br />getch();<br />}<br />Pointer Expressions:<br />A Pointer is a variable data type and hence the general rule to assign its value to the pointer is same as that of any other variable data type.<br />For example:<br />int x;<br />int *ptr;<br />ptr = &x;<br />The memory address of variable quot;
 X quot;
 is assigned to the pointer variable ptr1.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />void main ( )<br />{<br />int x;<br />int *ptr;<br />x = 10;<br />ptr = &x;<br />clrscr ( );<br />cout<<quot;
X=quot;
<<x<<quot;
and ptr =quot;
<<ptr<<endl;<br />cout<<quot;
X=quot;
<<x<<quot;
and *ptr =quot;
<<*ptr<<endl;<br />getch ( );<br />}<br />The Output:<br />X = 10 and ptr = 0x24ccfff4<br />X = 10 and *ptr = 10<br />POINTERS TO OBJECTS:<br />Object pointers are useful in creating objects at run time. We can also use an object pointer to access the public members of an objects. The objects for a class can be accessed using pointers.<br />The general form for declaring object using pointer is:<br />Class classneme<br />{<br />member variable declaration;<br />member function declaration and definition;<br />};<br />classname object1;<br />classname *ptr;<br />ptr = & object1;<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class computer<br />{<br />private:<br />int ram;<br />int speed;<br />char name[10];<br />public:<br />void read()<br />{<br />cout<<quot;
 Enter the value of ram, spesed, name:quot;
<<endl;<br />cin>>ram>>speed>>name;<br />}<br />void print()<br />{<br />cout<<quot;
 Name=quot;
<<name;<br />cout<<quot;
 Speed=quot;
<<speed<<quot;
MHzquot;
;<br />cout<<quot;
 Ram=quot;
<<ram<<quot;
MBquot;
;<br />}<br />};<br />void main()<br />{<br />computer c1;<br />computer *ptr;<br />ptr = &c1;<br />clrscr();<br />ptr->read();<br />ptr->print();<br />getch();<br />}<br />VIRTUAL FUNCTION:<br />Virtual function is a function which does not really exist. But appears real to some parts of a program.<br />It is used to call a particular function from derived class having more than one function with same name. For this the base class member function must be declared as virtual.<br />This is an application of polymorphism. Here, we use the pointer to base class to refer all the derived objects.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class base<br />{<br />public:<br />void display()<br />{<br />cout<<quot;
 Display Basequot;
;<br />}<br />virtual void show()<br />{<br />cout<<quot;
 Show Basequot;
;<br />}<br />};<br />class derived : public base<br />{<br />public:<br />void display()<br />{<br />cout<<quot;
 Display Derivedquot;
;<br />}<br />void show()<br />{<br />cout<<quot;
 Show Derivedquot;
;<br />}<br />};<br />int main()<br />{<br />base b;<br />derived d;<br />base *bptr;<br />cout<<quot;
bptr points to base quot;
;<br />bptr = &b;<br />bptr -> display();<br />bptr -> show();<br />cout<<quot;
 bptr points to Derived quot;
;<br />bptr = &d;<br />bptr -> display();<br />bptr -> show();<br />return 0;<br />}<br />Unformatted I/O<br />( i ) Input Operation:-<br />The input operations are carried out using the following Member function.<br />1. Overload Operator >><br />This operator is overloaded in the istream class and is used to give input from keyboard to program. The general form is<br />Cin>>var1>>var2>>........>>varn;<br />Ex:<br />int number;<br />float average;<br />cin>>number>>average;<br />2. get ( )<br />This function is used to input a single character to the program. This is a member function of istream class and is accessed with the help of the object cin. There are two types of get ( ) function.<br />a.get ( char )<br />b.get ( void )<br />get ( char ):<br />This function is used to assign the input character to its argument. The general form is<br />Syntax:cin.get ( variable-name );<br />Ex:char opt;<br />cin.get ( opt );<br />when cin.get ( opt ) is executed the given character input is assigned to the variable opt.<br />get ( void ):<br />This function is used to assign the input character to the variable on the left hand side. The general form is<br />Syntax:variable-name = cin.get ( )<br />Ex:char opt;<br />opt = cin.get ( )<br />when opt = cin.get ( ) is executed the given character is assigned to the variable opt on the left hand side.<br />3. getline ( )<br />This function is used to input a line of text to the program. This is a member function of istream class and is accessed with the help of the object cin. The general form is<br />Syntax:cin.getline ( variable-name,size);<br />Ex:char name[10];<br />cin.getline(name,10);<br />When cin.getline ( name, 10 ) is executed the given line of text size 9 is assigned to the variable name. One place is used for the terminator ‘’. The reading of the line ends when the new line character (  ) is reached or the size is reached.<br />( ii ) Output Operation:-<br />The output operation is carried out using the following member functions.<br />1. Overloaded Operator <<<br />This operator is overloaded in the ostream class and is used to output data from program to VDU. The general formis<br />cout<<outputlist;<br />Ex:int x = 10;<br />Float y = x + 20;<br />cout<<x<<y;<br />2. put ( )<br />This function is used to output a single character from program to the VDU. This is a member function of ostream class and is accessed with the help of the object cout. The general form is<br />cout.put ( variable-name );<br />Ex:char d = ‘a’;<br />…………..<br />cout.put ( d );<br />When cout.put ( d ) is executed this displays the character a on the VDU.<br />cout.put ( 35 );<br />This display the character # on the VDU. Because 35 is the ASCII value of hash (#)<br />3. write ( )<br />This function is used to output a line of textfrom program to VDU. This is a member function of ostream class and is accessed with the help of the object cout. The general form is<br />cout.write ( variable-name, size );<br />Ex:char name [10];<br />cout.write (name,10);<br />When cout.write(name,10) is executed the characters stored in the variable name is displayed on the VDU. If the size is grater than the length of the line then it displays beyond bounds of line.<br />Formatted console I/O:<br />  In c++ number of functions are available to format the output. These functions are the member functions of the class ios. They are<br />1.width ( )<br />2.precision ( )<br />3.fill ( )<br />4.setf ( )<br />5.unsetf ( )<br />1. width ( ):<br />This function is used to specify the width of the data to be printed. Since it is a member function, this is accessed with the help of the object cout. The general form is<br />Syntax:cout.width ( size );<br />25<br />Ex1:int a = 25;output:<br />cout.width ( 4 );<br />cout<<a;<br />Ex2:int a = 400;output:<br />400<br />cout.width ( 2 );<br />cout<<a;<br />Rules:<br />1.The value printed is right justified.<br />2.If the size of the width is less than the actual data width c++ automatically increases the size depending on the data size.<br />3.The include file iostream.h should be included in the program.<br />2. precision ( )<br />This function is used to specify the number of digits to be displayed after the decimal point. Since it is a member function this is accessed with the help of the object cout. The general form is<br />Syntax:cout.precision ( size );<br />Ex:float a = 4522.72597601;<br />cout<<a;<br />4522.7259<br />cout.precision ( 4 );output:<br />cout<<a;<br />4522.725976<br />cout.precision ( 7 );output:<br />cout<<a;<br />Rules:<br />1.Default precision size is 6.<br />2.default precision can be changed by giving our own precision size.<br />3.it won’t print the trailing zeros.<br />3. fill ( )<br />This function is used to print the given character in the unfilled position of the number printed. Since it is a member function, this is accessed with the help of the object cout. This is called filling and padding.<br />Syntax:cout.fill ( arg );<br />Ex:int a = 750;<br />***750<br />cout.fill (‘*’);output:<br />cout.width(6);<br />Rules:<br />1.Default fill is blank space.<br />2.If one character is used in the fill, it remains until we change it.<br />4. setf ( ) – Using bit field group<br />This function is used to set the flags and bit field group of the class ios. This is used to change the existing output settings. This is a member function of class ios.<br />Syntax:cout.setf(var1,var2);<br />Ex:int a = -46;<br />cout.fill(‘@’);output:<br />-@@@46<br />cout.setf ( ios::internal, ios::adjustifield );<br />cout.width ( 6 );<br />cout<<a;<br />Rules:<br />1.It is used to print the text left justified.<br />2.Numeric is printed right justified.<br />The tables given below lists the possible flag setting and its group.<br />Format requiredFlag (arg1)Bit-field (arg2)<br />Left-justified outputios :: leftios :: adjustfield<br />Right-justified outputios :: rightios :: adjustfield<br />Padding after sign ios :: internalios :: adjustfield<br />Or base indicator<br />(like +##20)<br />Scientific notationios :: scientificios :: floatfield<br />Fixed point notationios :: fixedios :: floatfield<br />Decimal baseios :: decios :: basefield<br />Octal baseios :: octios :: basefield<br />Hexadecimal baseios :: hexios :: basefield<br />setf ( ) – Without bit field group<br />This function is used to set the flags in the class ios. This is used to display the trailing zeros and the plus sign in the output. This is a member function of a class ios.<br />Syntax:cout.setf(flag);<br />Ex:float a = -100.000;<br />float b = 12.72;<br />cout.setf(ios :: showpoint);<br />cout.setf(ios :: showpos);<br />cout.width(10);output<br />-100.000<br />cout<<endl<<a;<br />cout.width(10);output<br />+12.72<br />cout<<endl<<b;<br />5. unsetf ( )<br />This function is used to clear the flags already set to their default settings.<br />FlagMeaning<br />ios :: showpointTo show trailing decimal point and zeroes<br />ios :: showposTo print + before positive numbers.<br />Managing Output with manipulators:<br />The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats. They provide the same features as that of the ios member functions and flags. They are<br />1.setw()<br />2.setprecision()<br />3.setfill()<br />4.flush<br />5.endl<br />1. setw()<br />This function is used to specify the width of data to be printed. This is equivalent to width().<br />Syntax:setw(size);<br />Ex:int a = 123;output:<br />123<br />cout<<setw(5)<<a;<br />2. setprecision()<br />This function is used to specify the number jof places after the decimal point. This is equivalent to precision().<br />Syntax:setprecision(size);<br />Ex:float y = 9873.273;<br />cout<<setw(10);output:<br />9873.27<br />cout<<setprecision(2)<<y;<br />3. setfill()<br />This function is used to print the given character jin the unfilled position jof the number printed. This is equivalent to fill().<br />Syntax:setfill(int var1);orsetfill(char var2);<br />Ex:int a = 567;<br />cout<<setfill(‘*’);output:<br />**567<br />cout<<setw(5)<<a;<br />int a = 567;<br />cout<<setfill(35);output:<br />##567<br />cout<<setw(5)<<a;<br />Where 35 is the ASCII value of the character #.<br />4. flush<br />This function is used to clear the output buffer.<br />Syntax:flush;<br />Ex:int a = 20;<br />cout<<a;<br />cout<<flush;<br />5. endl<br />This function brings the control to the new line and flushes the buffer while printing the data.<br />Syntax:endl;<br />Ex:int a = 10;<br />float b = 20.35;<br />cout<<a<<endl<<b;<br />Function for manipulation of File Pointers:<br />Normally the file pointer position is changed automatically during file processing. But we can control the file pointer movement with the help of the following member function. <br />1.seekg() – Moves get pointer (input) to a specified location.<br />2.seekp() – Moves put pointer (output) to a specified location.<br />3.tellg() – Gives the current position of the get pointer.<br />4.tellp() – Gives the current position of the put pointer.<br />The table given below shows the possible flags available:<br />FlagMeaning<br />ios :: begsets the pointer at the beginning of the file<br />ios :: cursets the pointer in the current position<br />ios :: endsets the pointer at the end of the file<br />1. seekg():<br />This function is used to move the input pointer to the position specified. The general form is<br />Syntax:filename-object . seekg (pos,flag);<br />Ex:fstream student;<br />student . open (“student.dat”, ios :: in);<br />student . seekg (-20, ios :: end);<br />When the last statement is executed, output file pointer is set at the end of the file and moves 20 byte backward from the end.<br />2. seekp():<br />This function is used to move the output pointer to the position specified. The general form is<br />Syntax:filename-object . seekp (pos, flag);<br />Ex:fstream student;<br />student . open (“student.dat”, ios :: out);<br />student . seekg (-20, ios :: end);<br />When the last statement is executed, output file pointer is set at the end of the file and moves 20 byte backward from the end.<br />3. tellg()<br />This function returns the byte number of the current position of the input file pointer. The general form is<br />Syntax:int var1 = filename-object . tellg();<br />Ex:fstream student;<br />student . open (“student . dat”, ios :: in);<br />student . seekg(20);<br />int p = student . tellg();<br />When the above statements are executed 20 is assigned to p.<br />4. tellp()<br />This function returns the byte number of the current position of the output pointer. The general form is<br />Syntax:int var1 = filename-object . tellp();<br />Ex:fstream student;<br />student . open (“student . dat”, ios :: out);<br />student . seekg(20);<br />int p = student . tellg();<br />When the above statements are executed 20 is assigned to p.<br />
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++
Notes on c++

More Related Content

What's hot

What's hot (20)

Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
M C6java2
M C6java2M C6java2
M C6java2
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
M C6java3
M C6java3M C6java3
M C6java3
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Python second ppt
Python second pptPython second ppt
Python second ppt
 
Python Data Types
Python Data TypesPython Data Types
Python Data Types
 
M C6java7
M C6java7M C6java7
M C6java7
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++  Langauage Training in Ambala ! BATRA COMPUTER CENTREC++  Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
 
Data Handling
Data HandlingData Handling
Data Handling
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
Python-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutabilityPython-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutability
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
C++ interview question
C++ interview questionC++ interview question
C++ interview question
 
Data types in python
Data types in pythonData types in python
Data types in python
 

Viewers also liked

#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class DiagramHadziq Fabroyir
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
 
Antenna wave propa_nd11_ec2353
Antenna wave propa_nd11_ec2353Antenna wave propa_nd11_ec2353
Antenna wave propa_nd11_ec2353rajinimani
 
Five components of communication
Five components of communicationFive components of communication
Five components of communicationjumar dimas
 
Operating System Lecture Notes
Operating System Lecture NotesOperating System Lecture Notes
Operating System Lecture NotesFellowBuddy.com
 
Antenna and Wave Propagation Assignment I
Antenna and Wave Propagation Assignment IAntenna and Wave Propagation Assignment I
Antenna and Wave Propagation Assignment IGouthaman V
 
Operating System Notes
Operating System NotesOperating System Notes
Operating System NotesNandan Raj
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage classkapil078
 
Communication skills week 4
Communication skills week 4Communication skills week 4
Communication skills week 4Wardah Azhar
 
fco-lecture-8086
fco-lecture-8086fco-lecture-8086
fco-lecture-808624happy24
 
Antennas And Wave Propagation
Antennas And Wave PropagationAntennas And Wave Propagation
Antennas And Wave Propagationguestac67362
 
Advanced Operating System Lecture Notes
Advanced Operating System Lecture NotesAdvanced Operating System Lecture Notes
Advanced Operating System Lecture NotesAnirudhan Guru
 
Components of communication
Components of communicationComponents of communication
Components of communicationShozab Rehman
 
Antennas and wave propagation
 Antennas and wave propagation Antennas and wave propagation
Antennas and wave propagationJupira Silva
 

Viewers also liked (20)

#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Antenna wave propa_nd11_ec2353
Antenna wave propa_nd11_ec2353Antenna wave propa_nd11_ec2353
Antenna wave propa_nd11_ec2353
 
Digital notes
Digital notesDigital notes
Digital notes
 
Executive summary
Executive summaryExecutive summary
Executive summary
 
Five components of communication
Five components of communicationFive components of communication
Five components of communication
 
Operating System Lecture Notes
Operating System Lecture NotesOperating System Lecture Notes
Operating System Lecture Notes
 
Antenna and Wave Propagation Assignment I
Antenna and Wave Propagation Assignment IAntenna and Wave Propagation Assignment I
Antenna and Wave Propagation Assignment I
 
class c++
class c++class c++
class c++
 
Operating System Notes
Operating System NotesOperating System Notes
Operating System Notes
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
 
Communication skills week 4
Communication skills week 4Communication skills week 4
Communication skills week 4
 
fco-lecture-8086
fco-lecture-8086fco-lecture-8086
fco-lecture-8086
 
operating system lecture notes
operating system lecture notesoperating system lecture notes
operating system lecture notes
 
Antennas And Wave Propagation
Antennas And Wave PropagationAntennas And Wave Propagation
Antennas And Wave Propagation
 
Awp
AwpAwp
Awp
 
Advanced Operating System Lecture Notes
Advanced Operating System Lecture NotesAdvanced Operating System Lecture Notes
Advanced Operating System Lecture Notes
 
C++ quik notes
C++ quik notesC++ quik notes
C++ quik notes
 
Components of communication
Components of communicationComponents of communication
Components of communication
 
Antennas and wave propagation
 Antennas and wave propagation Antennas and wave propagation
Antennas and wave propagation
 

Similar to Notes on c++

C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYRajeshkumar Reddy
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
C questions
C questionsC questions
C questionsparm112
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and AnswersDaisyWatson5
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentalsumar78600
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diarySHARDA SHARAN
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 

Similar to Notes on c++ (20)

C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
C questions
C questionsC questions
C questions
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
functions-.pdf
functions-.pdffunctions-.pdf
functions-.pdf
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 

Recently uploaded

ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 

Recently uploaded (20)

Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 

Notes on c++

  • 1. OBJECT ORIENTED PROGRAMMING IN C++<br />#. INTRODUCTION TO OBJECT ORIENTED PROGRAMMING:<br />Object oriented program development is a new programming style having real world thinking. It is not a programming technique. So each and every programmer has his own way of thinking. <br />1.object<br />2.class<br />3.data abstracation<br />4.data encapsulation<br />5.inheritance<br />6.polymorphism<br />7.dynamic binding<br />8.message passing<br />Object:<br />An object is defined as an entity that contain data and its related functions. The functions operate on that data. The objects may be either physical or logical. <br />Class:<br />A class is defined as a collection of objects with same type of data and functions. The functions of the class should be defined. With the help of the class we can create number of objects of type class.<br />Data abstraction:<br />Abstraction is defined as a grouping of essential details and ignoring other details. Data abstraction is defined as a named collection of data that describes a data object in a class <br />Data encapsulation:<br />Encapsulation is a technique used to protect the informations in an object from other objects. In an object data and its functions are encapsulated into a single entity. Because of this other objects and programs cannot aces the data in an object directly. This concept is called data encapsulation or data hiding<br />Inheritance:<br />Inheritance is defined as sharing of attributes and functions among classes based on a hierarchical or sequential relationship<br />Main class or supper class<br />Derived class or sub class<br />Polymorphism:<br />A function is said to be polymorphic, if it is applied to many different classes with different operation.<br />Dynamic binding:<br />Binding is defined as the connection between the function call and its corresponding program code to be executed. There are 2 types of binding<br />1.static binding<br />2.dynamic binding<br />Static binding:the binding occurs during compilation time<br />Dynamic binding: the binding occurs during run time. This is also called late binding.<br />Message passing:<br />Message passing is a process of locating and executing a function in response to a message. Locating means matching the given message with the list of available functions.<br />#. Structure in c++<br />Include file<br />Class definition<br />Data declaration<br />Member function definition<br />Main function<br />Object declaration<br />Body of main function<br />#include<iostream.h> ……………header file<br />class stu ……………class defintion<br />{<br />private:………………visibility modifier<br />int x,y;…………….data declaration<br />public:<br />void read()…………….member function <br />{<br />cout<<”enter the val”;<br />cin>>x>>y;<br />}<br />void print()<br />{<br />cout<<”x=”<<x;<br />cout<<”y=”<<y;<br />}<br />};<br />void main()…………….main function<br />{<br />stu a;…………..data(object) declaration<br />a.read();……….called member function<br />a.print();<br />}<br />#. Control structures<br />A large number of functions are used that pass messages, and process the data contained in objects. A function is set up to perform a task. When the task is complex, many different algorithms can be designed to achieve the same goal. Some are simple to comprehend, while others are not.<br /> 1.Decision making (or) conditional statement<br /> 2.Looping statement<br />Decision making statement<br />•DMS are used to skip or to execute a group of statements based on the result of some condition.<br />•The decision making statements are,<br />•(i) simple if statement<br />•(ii) if…else statement<br />•(iii) switch statement<br />Simple if statement:<br />•Simple if statement is used to execute or skip one statement or group of statements for a particular condition. <br />•The general form is<br />•If(test condition)<br />{<br />–Statement block;<br />}<br />Next statement;<br /> When this statement is executed, the computer first evaluates the value of the test condition. If the value is true, statement block and next statement are executed sequentially. If the value is false, statement block is skipped and execution starts from next statement.<br />Example:<br />•#include<iostream.h><br />•#include<conio.h><br />•Void main()<br />•{<br />–Int mark;<br />–Char grade;<br />–Clrscr();<br />–Cin>>mark>>grade;<br />–<br />–If(grade= =‘a’)<br />–{<br />–Mark=mark+10;<br />–}<br />–<br />–Cout<<mark;<br />–Getch( );<br />}<br />(ii) if …else statement<br />if..else statement is used to execute one group of statements if the test condition is true or other group if the test condition is false<br />The gentral form<br />If(test condition)<br />{<br />statement block-1;<br />}<br />else<br />{<br />statement block-2;<br />}<br />next statement;<br />when this statement is executed, the computer first evaluates the value of the test condition. If the value is true, statement block-1 is executed and the control is transferred to next statement. If the value is false, statement block-2 is executed and the control is transferred to next statement.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int mark;<br />clrscr();<br />cin>>mark;<br />if(mark=>35)<br />cout<<”passquot; ;<br />else<br />cout<<”fail”;<br />getch();<br />}<br />(iii) Switch statement;<br />Switch case is used for multiple branching. The switch statement checks the value of an expression against a list of integer or character constants. When match found, the statements associated with that constant are executed. If no match found the statements under default section are executed.<br />Switch(expression)<br />{<br />case label-1:<br />statement block-1;<br />break;<br />case label-2:<br />statement block-2;<br />break;<br />case label-3:<br />statement block-3;<br />break;<br />……………………………………….<br />………………………………………<br />case label-n:<br />statement block-n;<br />break;<br />default:<br />default statement;<br />break;<br />}<br />next statement<br />ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int day;<br />cout<<”enter the number is 1 to 7”;<br />cin>>day;<br />switch(day)<br />{<br />case 1:<br />cout<<”Sunday”;<br />break;<br />case 2:<br />cout<<”Monday”;<br />break;<br />case 3:<br />cout<<”Tuesday”;<br />break;<br />case 4:<br />cout<<”Wednesday”;<br />break;<br />case 5:<br />cout<<”Thursday”;<br />break;<br />case 6:<br />cout<<”Friday”;<br />break;<br />case 7:<br />cout<<”Saturday”;<br />break;<br />default:<br />cout<<”Enter the current val”;<br />break;<br />}<br />}<br />Looping Statements:<br />LS are used to execute a group of statements repeatedly unit some condition is satisfied. The looping statements are<br />(i)while statement<br />(ii)do..while statement<br />(iii)for statement<br />While statement:<br />While(test condition)<br />{<br />body of the loop;<br />}<br />next statement;<br />when this statement is executed, the computer first evaluates the test condition. If the value is false, the contron is transferred to next statement. if the value is true then the body of the loop is executed repeatedly until the test condition becomes false. When the test condition becomes false the control is transferred to next statement.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int i=1;<br />while (i<5)<br />{<br />cout<<”god is great”;<br />i++;<br />}<br />}<br />(ii) do…while statement<br />The general form:<br />do<br />{<br />body of the loop;<br />}<br />while(test condition);<br />next statement;<br />when this statement is executed the body of the loop is executed first. Then the test condition is evaluated. If the value is false, the control is transferred to the next statement. If the value is true the body of the loop is executed repeatedly until the test condition becomes false. When the test condition becomes false the control is transferred to the next statement.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />int i=1;<br />do<br />{<br />cout<<”god is love”;<br />i++;<br />}<br />while (i<5)<br />}<br />(iv)for statememt:<br />for statement is used to execute a statement or a group of statements repeatedly for a known number of times. The general form is<br />for(initial condition; test condition; increment or decrement)<br />{<br />body of the loop;<br />}<br />next statement;<br />when the for statement is executed the value of the control variable and tested with the test condition. If the value of the test condition is true, the body of the loop will be executed and the control is transferred to the for statement. Then the value of the control variablej is incremented or decremented. When the test condition becomes false the control is transferred to the next statement. The body of the loop is executed repeatedly as long as the test value is true<br />Ex:<br />#include<iostream.h><br />main()<br />{<br />int i,sum=0;<br />for (i=1;i<=50;i++)<br />{<br />sum=sum+i;<br />}<br />cout<<”sum=”<<sum;<br />}<br />FUNCTIONS:<br />Function is defined as a named group of statements. A function can be divided into small modules, then each module can be represented as a function. There are two types of function in c++.<br />1.library function<br />2.user defined functions<br />Library function:<br />LF are functions which are not required to be written by the programmer. But these are available in separate files and the programmer has to include it in the appropriate places.<br />Ex:<br />Cin>>,cout<<,etc.<br />User defined function:<br />A user defined function or simply function has to be written by the programmer to carry out some specific well defined task.<br />All c++ programs consist of one or more function. Out of this one function should be main. This function shows from where the program execution begins.<br />Function definition:<br />The general form is:<br />Function-type function-name ( list of argument)-> header functin<br />{<br />local variable declaration;<br />executable statement;-> statement body<br />return(expression);<br />}<br />A function should be defined before it is used. A function has two parts<br />1.function header<br />2.statement body<br />Rules:<br />1.Function header should not terminate with semicolon. <br />2.list of arguments and argument declaration are optional<br />3.if the function has no list of arguments an empty parentheses is a must<br />Declaring function-type:<br />The function type in the function header is optional. If there is no function type in the function header the return statement by default returns a integer value.<br />But with the help of the option function-type in the function header we can declare the type of the value returned by the function.<br />Ex:<br />1.function-name (list of arguments)<br /> - this function returns integer value<br />2.int function-name (list of arguments)<br /> - this function returns integer value<br />3.float function-name (list of arguments)<br /> - this function returns floating point value.<br />Function returning nothing:<br />If the function is not to return any value, we can declare the function of type void, which tells c++ not to save any temporary space for a value to be sent by the function.<br />The general form is:<br />Void function-name ( list of arguments)<br />Inline function:<br />Inline is a keyword prefixed with the function definition. Whenever this function is called the compiler inserts the statements in the function in the calling place.<br />The general form is:<br />Inline data-type function-name(argument list)<br />{<br />function body;<br />}<br />#include<iostream.h><br />#include<conio.h><br />class sample<br />{<br />private:<br />int a,b;<br />public:<br />void getdata()<br />{<br />cout<<”enter the value a and b:”;<br />cin>>a>>b;<br />}<br />void display();<br />};<br />inline void sample::display()<br />{<br />cout<<”The add is”<<a+b<<endl;<br />cout<”The sub is”<<a-b<<endl;<br />}<br />void main()<br />{<br />clrscr();<br />sample a;<br />a.getdata();<br />a.display();<br />getch();<br />}<br />Function Overloading:<br />If a single function performs different operations, then that function is called function overloading. Overloading can be implemented in two ways.<br />1.using different number of arguments<br />2.using different type of arguments<br />Different Numbers:<br />The function with same name can be defined in a same program as given below,<br />1.function with no argument<br />2.function with one argument<br />3.function with two argument<br />………………………………….<br />…………………………………<br />according to our need we have to individually declare and define the functions. When the function is called, depending upon the number of arguments the appropriate function will be executed. This type of implementation is called overloading the function.<br />Different Types:<br />The function with same name can be defined in the same program with same number of arguments with different data types. According to our need we have to individually declare and define the functions. When the function is called, depending upon the type of the argument the appropriate function will execute. This type of implementation is called overloading the function.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />int add(int a,int b)<br />{<br />return (a+b);<br />}<br />float add(int a, float b)<br />{<br />return(a+b);<br />}<br />float add(float a,float b)<br />{<br />return(a+b);<br />}<br />void main()<br />{<br />clrscr();<br />int a,b;<br />float c,d;<br />cout<<”Enter the int number of a,b”;<br />cin>>a>>b;<br />cout<<”Enter the float number of c,d”;<br />cin>>c>>d;<br />cout<<”The result is”;<br />cout<<add(a,b)<<endl;<br />cout<<add(a,c)<<endl;<br />cout<<add(c,d)<<endl;<br />getch();<br />}<br />STRUCTURES:<br />A structure is defined as a data type to represent different types of data with a single name. The data items in a structure are called members of the structure.<br />Defining a structure:<br />A structure definition contains a keyword struct and a user defined structure-name <br />The general form is:<br />Struct structure-name<br />{<br />data-type member-1;<br />data-type member-2;<br />……………………..<br />……………………<br />data-type member-n;<br />};<br />Ex:<br />Struct student<br />{<br />int number;<br />int age;<br />char sex;<br />};<br />Drawbacks in structures:<br />1.in structures we can keep only data. It is not possible to keep functions.<br />2.data hiding is not possible in structures.<br />Declaration:<br />Structure declaration means defining variables to the already defined structure. We can define variables in two ways.<br />1.variable definition along with the structure definition<br />2.variable definition using structure-name.<br />The general form is:<br />(i) First ways:<br />Struct structure-name<br />{<br />data-type member-1;<br />data-type member-2;<br />……………………..<br />……………………<br />data-type member-n;<br />}variable-1, variable-2,…..variable-n;<br />Ex:<br />Struct student<br />{<br />int number;<br />int age;<br />char sex;<br />}s1,s2,s3;<br />(ii) second ways:<br />Struct structure-name<br />{<br />data-type member-1;<br />data-type member-2;<br />……………………..<br />……………………<br />data-type member-n;<br />};<br />structure-name variable-1, variable-2,…..variable-n;<br />Ex:<br />Struct student<br />{<br />int number;<br />int age;<br />char sex;<br />};<br />student s1,s2,s3;<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />void main()<br />{<br />struct employee<br />{<br />char sex;<br />int number;<br />float salary;<br />}dept1;<br />cout<<”Give the values to the numbers”;<br />cout<<”Give the sex”;<br />cin>>dept1.sex;<br />cout<<”Give the number”;<br />cin>>dept1.number;<br />cout<<”Give the salary”;<br />cin>>dept1.salary;<br />cout<<”Employee-details”;<br />cout<<”Sex:”<<dept1.sex;<br />cout<<”Number:”<<dept1.number;<br />cout<<”Salary:”<<dept1.salary;<br />}<br />CLASS:<br />A class is a userdefined datatype. It contains data and its related functions. The data and functions in a class can be defined by any one of the following visibility modifiers.<br />1.public<br />2.private<br />3.protected<br />public:<br />if a data or function defined as public, it can be accessed from outside the class.<br />Private:<br />If a data or function is defined as private, it cannot be accessed from outside the class. This concept is called data hiding.<br />Protected:<br />If a data or function is defined as protected, it can be accessed only by the classes derived from this class.<br />Class definition:<br />A class definition contains a keyword class and a user defined class-name followed by data and functions named as members of the class. The members are enclosed with in braces.<br />Syntax:<br />Class class-name<br />{<br />private:<br />variable declaration;<br />function declaration or definition;<br />public:<br />variable declaration;<br />function declaration or definition;<br />protected:<br />variable declaration;<br />function declaration or definition;<br />};<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class fact<br />{<br />private:<br />int I,n,sum;<br />public:<br />void getdata()<br />{<br />cout<<”Enter the value n:”;<br />cin>>n;<br />}<br />void display()<br />{<br />I=1;<br />Sum=1;<br />While(I<=n)<br />{<br />sum=sum+I;<br />I++;<br />}<br />cout<<”Fact is”<<sum;<br />}<br />};<br />void main()<br />{<br />fact f;<br />clrscr();<br />f.getdata();<br />f.display();<br />getch();<br />}<br />Defining Member Function:<br />Member functions can be defined in two places:<br />i.Outside the class definition.<br />ii.Inside the class definition<br />Outside the class definition:<br />Member functions that are declared inside a class have to be defined separately outside the class. Their definitions are very much like the normal functions. An important difference between a member function and a normal function is that a member function incorporates a membership ‘identity label’ in the header. This ‘label’ tells the compiler which class the function belongs to.<br />The general form is:<br />Return-type class-name :: function-name (argument declaration)<br />{<br />function body;<br />}<br />Ex:<br />Void item :: getdata(int a, float b)<br />{<br />number = a;<br />cost = b;<br />}<br />Inside the class definition:<br />Another method of defining a member function is to replace the function declaration by the actual function definition inside the class.<br />Ex:<br />Class item<br />{<br />int no;<br />float cost;<br />public:<br />void getdata(int a,float b);<br />void putdata(void)<br />{<br />cout<<no<<””;<br />cout<<cost<<””;<br />}<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class item<br />{<br />int number;<br />float cost;<br />public:<br />void getdata(int a,float b);<br />void putdata(void)<br />{<br />cout<<”Number:”<<number<<””;<br />cout<<”Cost:”<<cost<<””;<br />}<br />};<br />void item :: getdata(int a, float b)<br />{<br />number =a;<br />cost =b;<br />}<br />int main( )<br />{<br />item x;<br />cout<<” object x”<<””;<br />x.getdata(100,299.95);<br />x.putdata( );<br />item y;<br />cout<<” object y”<<””;<br />y.getdata(200, 175.50);<br />y.putdata();<br />return 0;<br />}<br />Array within a Class:<br />The arrays can be used as member variables in a class. The following class definition is valid<br />Class array<br />{<br />int a[size];<br />public:<br />void setval(void);<br />void display(void);<br />};<br />the array variable a[] declared as a private member of the class array can be used in the member functions, like any other array variable. We can perform any operations on it. For instance, in the above class definition, the member function setval() sets the values of elements of the array a[], and display() function display the values.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class array<br />{<br />private:<br />int a[10];<br />int size;<br />public:<br />void read();<br />int sum();<br />};<br />void array :: read()<br />{<br />int I;<br />cout<<” enter the size:”;<br />cin>>size;<br />cout<<” enter the element one by one:”;<br />for(I=0;I<size;I++)<br />cin>>a[I];<br />}<br />int array :: sum()<br />{<br />int s=0,I;<br />for(I=0;I<size;I++)<br />s += a[I];<br />return s;<br />}<br />void main()<br />{<br />int sum;<br />array b;<br />b.read();<br />b.display();<br />sum=b.sum();<br />cout<<” sum=”<<sum;<br />}<br />Array of Objects:<br />With the help of arrays, more than one object in a class in a class can be defined by a single name. This is called array of objects.<br />The general form is:<br />Class-name array-name [size];<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class employee<br />{<br />char name[10];<br />float age;<br />public:<br />void getdata(void);<br />void putdata(void);<br />};<br />void employee :: getdata (void)<br />{<br />cout<<”Enter name:”;<br />cin>>name;<br />cout<<”Enter age:”;<br />cin>>age;<br />}<br />void employee :: putdata (void)<br />{<br />cout<<”Name:”<<name<<””;<br />cout<<”Age:”<<age<<””;<br />}<br />const int size=3;<br />int main()<br />{<br />employee manager[size];<br />for(int I=0;I<size;I++)<br />{<br />cout<<” Details of manager”<<I+1<<””;<br />manager[I].getdata();<br />}<br />cout<<””;<br />for(I=0;I<size;I++)<br />{<br />cout<<” manager”<<I+1<<””;<br />manager[I].putdata();<br />}<br />return 0;<br />}<br />Friend Function:<br />In a class, private member variables can be accessed only by the member functions in that class. But with the help of friend function we can access the private member variables in a class. The friend function should be a non-member class<br />Thus friendly functions are non-member function which are used to access the private member variable in a class.<br />The general form is:<br />Class class-name<br />{<br />private:<br />……….<br />………<br />public:<br />………<br />……….<br />Friend return-type non-member function name(argument list);<br />};<br />Rules:<br />1.The keyword friend can be used only inside the class.<br />2.More than one friend function can be declared in a class.<br />3.A function can be friend to more than one class.]<br />4.The friend function definition should not contain the scope operator.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class sample<br />{<br />int a;<br />int b;<br />public:<br />void setvalue()<br />{<br />a=25;<br />b=40;<br />}<br />friend float mean(sample s);<br />};<br />float mean(sample s)<br />{<br />return float(s.a+s.b)/2.0;<br />}<br />int main()<br />{<br />sample x;<br />x.setvalue();<br />cout<<”Mean value=”<<mean(x)<<””;<br />return 0;<br />}<br />Constructors<br />Constructor is a special member function. This is used to give initial values to member variables. The constructor is invoked whenever an object of its associated class is created.<br />The general form is:<br />Constructor name (parameters)<br />{<br />to give initial values to member variables;<br />}<br />Rules:<br />1.It should be declared as public<br />2.No return type needed.<br />3.When the object is declared for the class, it automatically executes the constructor and initialize the variables.<br />There are 3 types of constructors.<br />(i)Constructors without parameters<br />(ii)Constructors with paramerters<br />(iii)Multiple constructors.<br />Constructors without parameters:<br />If a constructor has no arguments then it is called constructor without paramerter.<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class abc<br />{<br />int i;<br />public:<br />abc()<br />{<br />i=10;<br />clrscr();<br />}<br />void print()<br />{<br />cout<<quot; value=quot; <<i;<br />}<br />void incre()<br />{<br />i++;<br />}<br />};<br />void main()<br />{<br />abc c1,c2;<br />c1.print();<br />c2.print();<br />c1.incre();<br />c1.incre();<br />c2.incre();<br />c1.print();<br />c2.print();<br />getch();<br />}<br />Constructors with parameters:<br />If a constructor has arguments then it is called constsructor with parameters. The value of the arguments should be given along the object declaration.<br />The general form is<br />Class-name object ( argument-value-list);<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class item<br />{<br />private:<br />int item_code;<br />float item_price;<br />public:<br />item()<br />{<br />item_code=0;<br />item_price=0.0;<br />}<br />item(int x, float y)<br />{<br />item_code=x;<br />item_price=y;<br />clrscr();<br />}<br />void get_data()<br />{<br />cout<<quot; enter the code:quot; ;<br />cin>>item_code;<br />cout<<quot; enter the price:quot; ;<br />cin>>item_price;<br />}<br />void disp_data()<br />{<br />cout<<quot; item code=quot; <<item_code;<br />cout<<quot; itemprice=quot; <<item_price;<br />}<br />};<br />void main()<br />{<br />item obj1;<br />item obj2(2,10.50);<br />obj1.disp_data();<br />obj2.disp_data();<br />obj1.get_data();<br />obj1.disp_data();<br />getch();<br />}<br />Multiple constructors:<br />If a class has more than one constructor, then it is called multiple constructors. This technique is called overloading constructors.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class con<br />{<br />int a,b,c;<br />public:<br />con(int x);<br />con(int x, int y);<br />con(int x, int y, int z);<br />};<br />con :: con(int x)<br />{<br />a = x;<br />cout<<quot; Value of A is:quot; <<a<<endl;<br />}<br />con :: con(int x, int y)<br />{<br />a = x;<br />b = y;<br />cout<<quot; Value of A is:quot; <<a<<endl;<br />cout<<quot; Value of B is:quot; <<b<<endl;<br />}<br />con :: con(int x, int y, int z)<br />{<br />a = x;<br />b = y;<br />c = z;<br />cout<<quot; Value of A is:quot; <<a<<endl;<br />cout<<quot; Value of B is:quot; <<b<<endl;<br />cout<<quot; Value of C is:quot; <<c<<endl;<br />}<br />void main()<br />{<br />clrscr();<br />cout<<quot; Creating Object With One Valuequot; <<endl;<br />con one(10);<br />cout<<quot; Creating Object With Two Valuequot; <<endl;<br />con two(100,200);<br />cout<<quot; Creating Object With Three Valuequot; <<endl;<br />con three(1000,2000,3000);<br />getch();<br />}<br />Constructor with default argument:<br />If we give initial values to the member variables inside the argument list of constructor then it is called constructor with default arguments.<br />The general form is:<br />Constructor-name ( argument-type argument-variable = value1, …);<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />#include<string.h><br />class abc<br />{<br />int x;<br />float y;<br />char z[5];<br />public:<br />abc(int x1=10, float y1=20.0, char z1[10]=quot; Raamquot; );<br />void read()<br />{<br />cout<<quot; Enter x,y,z:quot; ;<br />cin>>x>>y>>z;<br />}<br />void print()<br />{<br />cout<<quot; valuej of x=quot; <<x;<br />cout<<quot; valuej of y=quot; <<y;<br />cout<<quot; valuej of z=quot; <<z;<br />}<br />};<br />abc :: abc(int x1, float y1, char z1[])<br />{<br />x=x1;<br />y=y1;<br />strcpy(z,z1);<br />clrscr();<br />}<br />void main()<br />{<br />abc a1;<br />abc a2(5);<br />abc a3(100, 12.30);<br />abc a4(20, 16.39, quot; Rajaquot; );<br />a1.print();<br />a2.print();<br />a3.print();<br />a4.print();<br />a4.read();<br />a4.print();<br />getch();<br />}<br />DYNAMIC INITIALISATIONJOF OBJEECTS:<br />Dynamic initialization of objects means giving initial values to the member variables during run time. With the help of this facility, we can give different type of values to the member variables depending upon the situation. <br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />int no;<br />float weight;<br />int age;<br />public:<br />student()<br />{<br />no=0;<br />weight=0.0;<br />age=0;<br />}<br />student(int n1,float w1,int a1);<br />student(int n2,int w2,float a2);<br />void print()<br />{<br />cout<<quot; Number=quot; <<no;<br />cout<<quot; weight=quot; <<weight;<br />cout<<quot; age=quot; <<age;<br />}<br />};<br />student :: student (int n1,float w1,int a1)<br />{<br />no=n1;<br />weight=w1;<br />age=a1;<br />}<br />student :: student(int n2,int w2, float a2)<br />{<br />no=n2;<br />weight=w2;<br />age=a2;<br />}<br />void main()<br />{<br />student s1,s2;<br />int n,a,w1;<br />float w,a1;<br />clrscr();<br />s1.print();<br />cout<<quot; enter number, weight, age:quot; ;<br />cin>>n>>w>>a;<br />s1=student(n,w,a);<br />s1.print();<br />cout<<quot; enter thenumber,weight,age:quot; ;<br />cin>>n>>w1>>a1;<br />s2=student(n,w1,a1);<br />s2.print();<br />getch();<br />}<br />COPY CONSTRUCTORS:<br />Copy constructors are used to declare and initialize an object with the values from another object. A copy constructor takes a reference to an object of the same class as itself as an argument. <br />Declaration:<br />Constructor-name ( class-name &object-name )<br /> {<br /> ……………….<br /> ……………….<br /> }<br />Copy constructor calling:<br />Class-name new-obj-name ( old-obj-name );<br />Or<br />Class-name new-obj-name = old-obj-name;<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />int no;<br />int mark;<br />public:<br />student (int n1,int m1)<br />{<br />no = n1;<br />mark = m1;<br />}<br />student (student &a1)<br />{<br />no = a1.no;<br />mark = a1.mark;<br />}<br />void print()<br />{<br />cout<<quot; No=quot; <<no;<br />cout<<quot; Mark=quot; <<mark;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />student s1(20,400);<br />student s2=s1;<br />student s3(10,500);<br />student s4(s3);<br />s1.print();<br />s2.print();<br />s3.print();<br />s4.print();<br />getch();<br />}<br />DYNAMIC CONSTRUCTORS:<br />If the memory space for the constructor (memory variables) is allocated during the constructor calling, then it is called dynamic constructor. This is done with the help of operator new. Depending on the size of the values in the variable the memory occupation of the objects varies.<br />Examples:<br />#include<iostream.h><br />#include<conio.h><br />#include<string.h><br />class string<br />{<br />char *name;<br />int length;<br />public:<br />string()<br />{<br />length=0;<br />name = new char [length + 1];<br />}<br />string (char *s)<br />{<br />length=strlen(s);<br />name = new char [length +1];<br />strcpy (name,s);<br />}<br />void display(void)<br />{<br />cout<<name<<quot; quot; ;<br />}<br />void join(string &a, string &b);<br />};<br />void string :: join(string &a, string &b)<br />{<br />length = a.length + b.length;<br />delete name;<br />name = new char [length + 1];<br />strcpy(name,a.name);<br />strcat(name,b.name);<br />};<br />int main()<br />{<br />char *first = quot; josephquot; ;<br />clrscr();<br />string name1(first), name2(quot; louisquot; ),name3(quot; lagrangequot; ),s1,s2;<br />s1.join(name1, name2);<br />s2.join(s1, name3);<br />name1.display();<br />name2.display();<br />name3.display();<br />s1.display();<br />s2.display();<br />getch();<br />return 0;<br />}<br />Destructors:<br />Destructor is a member function used to deallocate the memory space allocated by the constructor. A destructor is a function that automatically executes when an object is destroyed. A destructor function gets executed whenever an instance of the class to which it belongs goes out of existence. <br />The general form is<br />~ destructor-name()<br />{<br /> …………… <br /> ……………<br />}<br />~ ( tilde)-> destructor operator<br />destructor-name-> name of the class<br />The complier automatically deallocates the memory space when the program ends.<br />Rules:<br />1.It won’t take any arguments<br />2.It won’t return any value<br />3.It cannot be declared static, const<br />4.It should have public access in the class declaration.<br />5.If new operator is used inside the constructor for allocating memory, delete operator should be used inside the destructor to free the memory<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class baseA<br />{<br />public:<br />baseA()<br />{<br />cout<<”base class constructor”;<br />}<br />~baseA()<br />{<br />cout<<”base class destructor”;<br />}<br />};<br />class derivedD :public baseA<br />{<br />derivedD()<br />{<br />cout<<”derived class constructor”;<br />}<br />~derivedD()<br />{<br />cout<<”derived class destructor”;<br />}<br />};<br />void main()<br />{<br />derivedD obj;<br />}<br />Operator Overloading:<br />Operator Overloading is a technique used to change the predefined operation of an operator for existing data types to a new operation for user defined data types.<br />All the operators can be overloaded except the operators given below.<br />1.Conditional operator ?:<br />2.Scope resolution operator ::<br />3.Class member operator .*<br />4.Size of operator sizeof<br />5.Membership operator .<br />Defining operator overloading:<br />Overloading can be defined inside the class or outside the class. If defined inside, the declaration and definition are same. If defined outside, it should be declared inside using prototype.<br />The general form is:<br />Defining inside the class:<br />Return-type operator symbol ( argument list )<br />{<br />...............<br />...............<br />}<br />Defining outside the class:<br />Declaration inside the class:<br />Return-type operator symbol ( list of argument type);<br />Definition outside the class<br />Return-type class-name :: operator symbol ( list of argument type)<br />{<br />................<br />................<br />}<br />Rules:<br />1.Operator overloading function should be a public member function or friend function.<br />2.Unary ++, -- can be called by prefix or post fix form.<br />Unary Operator Loading:<br />If the symbol used in a operator overloading function is a unary operator (-, +, ++, --, etc) then this overloading is called unary operator overloading.<br />Let us consider the unary minus operator. A minus operator when used as a unary, takes just one operand. We know that this operator changes the sign of an operand when applied to a basic data item. <br />The general form is:<br />Return-type operator unary-symbol ( )<br />{<br />...............<br />}<br />Calling the function:<br />Symbol object-name;<br />When this statement is executed, the symbol calls the overload function and the object value is changed depending jupon the value of the overload function.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class space<br />{<br />int x,y,z;<br />public:<br />void getdata(int a, int b, int c);<br />void display(void);<br />void operator-();<br />};<br />void space :: getdata(int a, int b, int c)<br />{<br />x=a;<br />y=b;<br />z=c;<br />}<br />void space :: display(void)<br />{<br />cout<<x<<quot; quot; ;<br />cout<<y<<quot; quot; ;<br />cout<<z<<quot; quot; ;<br />}<br />void space :: operator - ()<br />{<br />x=-x;<br />y=-y;<br />z=-z;<br />}<br />int main()<br />{<br />space s;<br />clrscr();<br />s.getdata(10,-20,30);<br />cout<<quot; s:quot; ;<br />s.display();<br />-s;<br />cout<<quot; s:quot; ;<br />s.display();<br />getch();<br />return 0;<br />}<br />Binary Operator Loading:<br />If the symbol used in a operator overloading function is a binary operator (+,-,*,/,+=,-=,etc) then this overloading is called binary operator overloading. This takes only one argument from argument list and the other from the calling object.<br />The general form is:<br />Return-type operator binary-symbol (data-type1)<br />{<br />..............<br />}<br />Calling the overloaded function:<br />The general form is:<br />Object3 = object1 binary-symbol object2<br />When this statement is executed object1 calls the overloading symbol and passes the object2 as a argument to the overload function. Finally the calculated value will be assigned to object3 in the left hand side.<br />Rules:<br />1.It requires one argument explicitly.<br />2.The symbol is the overload function and calling must be same.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class complex<br />{<br />float x,y;<br />public:<br />complex()<br />{<br />}<br />complex(float real, float img)<br />{<br />x=real;<br />y=img;<br />}<br />complex operator + (complex c);<br />void display(void);<br />};<br />complex complex :: operator + (complex c)<br />{<br />complex temp;<br />temp.x = x+c.x;<br />temp.y = y+c.y;<br />return(temp);<br />}<br />void complex :: display(void)<br />{<br />cout<<x<<quot; +jquot; <<y<<quot; quot; ;<br />}<br />int main()<br />{<br />complex c1,c2,c3;<br />clrscr();<br />c1 = complex(2.5,3.5);<br />c2 = complex(1.6,2.7);<br />c3 = c1 + c2;<br />cout<<quot; c1=quot; ;<br />c1.display();<br />cout<<quot; c2=quot; ;<br />c2.display();<br />cout<<endl;<br />cout<<quot; c3=quot; ;<br />c3.display();<br />getch();<br />return 0;<br />}<br />Manipulation of Strings Using Operators:<br />In c++ there are no operators available for string manipulation. Butwe can overload the operators for doing the required string operation (concatination, comparision, ect.,)<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />#include<string.h><br />class string1<br />{<br />private:<br />char str[40];<br />public:<br /> void getstr()<br /> {<br />cout<<quot; Enter a string:quot; ;<br />cin>>str;<br />}<br /> string1 operator + (string1 str1)<br /> {<br /> string1 str3;<br /> strcpy(str3.str,(strcat(str,str1.str)));<br /> return(str3);<br /> }<br /> void printstr()<br /> {<br /> cout<<str;<br /> }<br />};<br />void main()<br />{<br />string1 a,b,c;<br />clrscr();<br />a.getstr();<br />b.getstr();<br />cout<<quot; The first string is:quot; ;<br />a.printstr();<br />cout<<quot; The 2nd string is:quot; ;<br />b.printstr();<br />cout<<quot; The concatenation string is:quot; ;<br />c = a + b;<br />c.printstr();<br />getch();<br />}<br />Rules for Overloading Operators:<br />1.Only existing operators can be overloaded. New operators cannot be created.<br />2.We cannot change the basic meaning of an operator. That is, we cannot redefine the ( * ) operator to add one value to another.<br />3.We cannot overload all operators. The operators which we cannot overload are sizeof, .*, ::, ., ?:<br />4.Symbol used in the overload function andjin the function call must be unique.<br />5.Binary operator overloading using member function requires one argument explicitly. But using friend function requires two arguments explicitly.<br />6.We cannot overload all operators using friend function. <br />They are =, ( ), [ ], -><br />7.The syntax rule for original operator and overloaded operator are same.<br />INHERITANCE:<br />Inheritance is the process of creating new classes from the existing classes. the new classes are called derived classes. the existing classes are called base classes.<br />The derived classes inherit all properties of the base classes plus its own properties. the process of inheritance does not affect the base classes.<br />Advantages:<br />1. New classes can be derived by the user from the existing classes without any modification.<br />2. It saves time and money.<br />3. It reduces program coding time<br />4. It increases the reliability of the program.<br />Syntax:<br />class derived_class_name : mode base_class_name<br />{<br /> new member variable declaraction;<br /> ...................................<br /> ....................................<br /> new member function declaracion and definition<br /> ...................................<br /> ...................................<br />};<br />SINGLE INHERITANCE:<br />If a derived class is derived from a single base class, then it is called single inheritance. single inheritance can be derived publicly or privately.<br />If publicly derived, the public and protected member functions of the base class becomes public and protected in the derived class. if privately derived the public and protected member functions of the base class becomes private in the derived class.<br />Syntax:<br />class base<br />{<br />..............<br />..............<br />};<br />class derive 1 : mode base<br />{<br />..............<br />..............<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />private:<br />int reg_no;<br />protected:<br />char name[20];<br />public:<br />void read()<br />{<br />cout<<quot; Enter the reg_no:quot; ;<br />cin>>reg_no;<br />cout<<quot; Enter the name:quot; ;<br />cin>>name;<br />}<br />void print()<br />{<br />cout<<quot; Reg_no=quot; <<reg_no;<br />}<br />};<br />class computer:public student<br />{<br />private:<br />int roll_no;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot; Enter the roll_no:quot; ;<br />cin>>roll_no;<br />}<br />void print1()<br />{<br />cout<<quot; roll_no=quot; <<roll_no;<br />cout<<quot; Name=quot; <<name;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />computer s1;<br />s1.read1();<br />s1.print();<br />s1.print1();<br />getch();<br />}<br />MULTILEVEL INHERITANCE:<br />Multilevel inheritance is a process to derive classes in a sequential order. A base class is treated as level 1 class. from this class we can derive a new class and is treated as level 2 class. from the level 2 class we can derive another new class and is treated as level 3 class and so on.<br />The lower level class inherits the higher level classes along the inheritance path.<br />Syntax:<br />class base<br />{<br />..............<br />..............<br />};<br />class derive 1 : mode base<br />{<br />..............<br />..............<br />};<br />class derive 2 : mode derive 1<br />{<br />...........<br />...........<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[12];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot; Enter the number and model:quot; ;<br />cin>>reg_no>>model;<br />}<br />void print()<br />{<br />cout<<quot; Reg_no=quot; <<reg_no;<br />cout<<quot; Model=quot; <<model;<br />}<br />};<br />class two_wheeler : public vehicle<br />{<br />protected:<br />int no_gear;<br />int power;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot; Enter the number of gear,power:quot; ;<br />cin>>no_gear>>power;<br />}<br />void print1()<br />{<br />print();<br />cout<<quot; NO_gear=quot; <<no_gear;<br />cout<<quot; power=quot; <<power;<br />}<br />};<br />class scotter:public two_wheeler<br />{<br />protected:<br />char manufacturer[20];<br />char owner[20];<br />public:<br />void read2()<br />{<br />read1();<br />cout<<quot; Enter the name of manufacturer and owner:quot; ;<br />cin>>manufacturer>>owner;<br />}<br />void print2()<br />{<br />print1();<br />cout<<quot; Manufacturer name=quot; <<manufacturer;<br />cout<<quot; owner name=quot; <<owner;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />scotter s1;<br />s1.read2();<br />s1.print2();<br />getch();<br />} <br />MULTIPLE INHERITANCE:<br />A class derived from more than one base classes is called multiple inheritance.<br />Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. it is like a child inheriting the physical features of one parent and the intelligence of another.<br />Syntax:<br />class base 1<br />{<br />................<br />................<br />};<br />class base 2<br />{<br />................<br />................<br />};<br />class base 3<br />{<br />................<br />................<br />};<br />class derived : mode base 1, mode base 2, mode base 3<br />{<br />................<br />................<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class income<br />{<br />protected:<br />float amount;<br />public:<br />void read()<br />{<br />cout<<quot; give the amount:quot; ;<br />cin>>iamount;<br />}<br />};<br />class expenditure<br />{<br />protected:<br />float eamount;<br />public:<br />void read1()<br />{<br />cout<<quot; give the amount:quot; ;<br />cin>>eamount;<br />}<br />};<br />class n_income:public income, public expenditure<br />{<br />private:<br />float namount;<br />public:<br />void print()<br />{<br />cout<<quot; income=quot; <<iamount;<br />cout<<quot; expenditure=quot; <<eamount;<br />cout<<quot; net amount=quot; <<namount;<br />}<br />void read2()<br />{<br />read();<br />read1();<br />namount=iamount-eamount;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />n_income n1;<br />n1.read2();<br />n1.print();<br />getch();<br />}<br />HIERARCHY INHERITANCE:<br />Hierarchy inheritance is a process to derive classes in a hierarchical order. A base class is treated as level 1 class. From this class we can derive new classes and is treated as level 2 classes. from a level 2 class we can derive a another new classes and is treated as level 3 and so on.<br />The lower level classes inherits the higher level classes along inheritance path.<br />Syntax:<br />class base<br />{<br />..............<br />..............<br />};<br />class derive 1 : mode base<br />{<br />..............<br />..............<br />};<br />class derive 2 : mode base<br />{<br />...........<br />...........<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[10];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot; Enter the reg_no, and model:quot; ;<br />cin>>reg_no>>model;<br />}<br />void print()<br />{<br />cout<<quot; Reg_no=quot; <<reg_no;<br />cout<<quot; Model=quot; <<model;<br />}<br />};<br />class two_wheeler : public vehicle<br />{<br />protected:<br />int no_gear;<br />int power;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot; Enter the number of gear and power:quot; ;<br />cin>>no_gear>>power;<br />}<br />void print1()<br />{<br />print();<br />cout<<quot; No_gear=quot; <<no_gear;<br />cout<<quot; Power=quot; <<power;<br />}<br />};<br />class scooter : public two_wheeler<br />{<br />protected:<br />char manufacturer[20];<br />char owner[20];<br />public:<br />void read2()<br />{<br />read1();<br />cout<<quot; Enter the name of manufacturer and owner:quot; ;<br />cin>>manufacturer>>owner;<br />}<br />void print2()<br />{<br />print1();<br />cout<<quot; Manufacturer:quot; <<manufacturer;<br />cout<<quot; Owner:quot; <<owner;<br />}<br />};<br />class four_wheeler : public vehicle<br />{<br />protected:<br />char fuel[20];<br />int no_cylinder;<br />public:<br />void read3()<br />{<br />read();<br />cout<<quot; give the type of fuel and no.of cylinder:quot; ;<br />cin>>fuel>>no_cylinder;<br />}<br />void print3()<br />{<br />print();<br />cout<<quot; fuel type=quot; <<fuel;<br />cout<<quot; number of cylinder=quot; <<no_cylinder;<br />}<br />};<br />class car : public four_wheeler<br />{<br />protected:<br />char name[20];<br />char owner[10];<br />public:<br />void read4()<br />{<br />read3();<br />cout<<quot; enter the name and owner:quot; ;<br />cin>>name>>owner;<br />}<br />void print4()<br />{<br />print3();<br />cout<<quot; name=quot; <<name;<br />cout<<quot; owner=quot; <<owner;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />scooter s1;<br />car c1;<br />s1.read2();<br />s1.print2();<br />c1.read4();<br />c1.print4();<br />getch();<br />}<br />HIBRID INHERITANCE:<br />An inheritance class derived from a base class and an inheritance class or two inheritance classes is called hybrid inheritance.<br />- Ex: (Multilevel, Multiple Inheritance):<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[10];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot; Enter the reg_no and model:quot; ;<br />cin>>reg_no>>model;<br />}<br />};<br />class four_wheeler : public vehicle<br />{<br />protected:<br />int no_cylinder;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot; enter the no. of cylinder:quot; ;<br />cin>>no_cylinder;<br />}<br />};<br />class car : public four_wheeler<br />{<br />protected:<br />char name[10];<br />char owner[20];<br />int no_seats;<br />float price1;<br />public:<br />void read2()<br />{<br />read1();<br />cout<<quot; enter the name, owner, no_seats, prince1:quot; ;<br />cin>>name>>owner>>no_seats>>price1;<br />}<br />};<br />class ac<br />{<br />protected:<br />float capacity;<br />char make[20];<br />float price2;<br />public:<br />void read3()<br />{<br />cout<<quot; Enter capacity, make, price2:quot; ;<br />cin>>capacity>>make>>price2;<br />}<br />};<br />class ac_car:public car, public ac<br />{<br />protected:<br />float total_price;<br />float fitting_charge;<br />public:<br />ac_car()<br />{<br />fitting_charge = 1000.0;<br />}<br />void read4()<br />{<br />read2();<br />read3();<br />total_price=price1+price2;<br />}<br />void print()<br />{<br />cout<<quot; Reg_no=quot; <<reg_no;<br />cout<<quot; Model=quot; <<model;<br />cout<<quot; No_cylinder=quot; <<no_cylinder;<br />cout<<quot; Name=quot; <<name;<br />cout<<quot; Owner=quot; <<owner;<br />cout<<quot; NO_Seats=quot; <<no_seats;<br />cout<<quot; Total_price=quot; <<total_price+fitting_charge;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />ac_car a1;<br />a1.read4();<br />a1.print();<br />getch();<br />}<br />AMBIGUITY:<br />Whenever a data member and member function are defined with the same name in both the base and the derived classes, these names must be without ambiguity. The scope resolution operator ( :: ) may be used to refer to any base member explicitly. This allows access to a name that has been redefined in the derived class.<br />Two base classes have functions with the same name, and also the same function name in derived class, we have ambiguity. The compiler which function is accessed from where.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class a<br />{<br />public:<br />void display()<br />{<br />cout<<quot; I am in class Aquot; ;<br />}<br />};<br />class b<br />{<br />public:<br />void display()<br />{<br />cout<<quot; I am in class Bquot; ;<br />}<br />};<br />class c:public a,public b<br />{<br />};<br />void main()<br />{<br />clrscr();<br />c obj;<br />obj.display(); // ambiguity compilation error<br />obj.a::display();<br />obj.b::display();<br />getch();<br />}<br />VIRTUAL BASE CLASS<br />When a class is made a virtual base class, c++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exist between the virtual base class and a derived class<br />The 'child' has two direct base classes 'parent1' and 'parent2' which themselvces have a common base clas 'grandparent'. the 'child' inherits the gtraits of 'grandparent' via two separate paths. it can also inherit directly as shown by the broken line. the 'grandparent' is sometimes referred to as indirect base class.<br />Syntax:<br />class a<br />{<br />.............<br />.............<br />};<br />class b1 : virtual public a<br />{<br />...............<br />...............<br />};<br />class b2 : virtual public a<br />{<br />..............<br />..............<br />};<br />class c : public b1, public b2<br />{<br />...............<br />..........<br />};<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class vehicle<br />{<br />protected:<br />char reg_no[10];<br />int model;<br />public:<br />void read()<br />{<br />cout<<quot; Enter the REG_NO and MODEL:quot; ;<br />cin>>reg_no>>model;<br />}<br />};<br />class road_vehicle : virtual public vehicle<br />{<br />protected:<br />int no_wheel;<br />char fuel[10];<br />public:<br />void read1()<br />{<br />cout<<quot; Enter the WHEELS and FUEL:quot; ;<br />cin>>no_wheel>>fuel;<br />}<br />};<br />class water_vehicle : virtual public vehicle<br />{<br />protected:<br />int no_leaf;<br />char fuel1[10];<br />public:<br />void read2()<br />{<br />cout<<quot; Enter the NO_OF LEAF and FUEL:quot; ;<br />cin>>no_leaf>>fuel1;<br />}<br />};<br />class road_water_vehicle : public road_vehicle,public water_vehicle<br />{<br />private:<br />int status;<br />public:<br />void read3()<br />{<br />read();<br />read1();<br />read2();<br />cout<<quot; Enter the Status: 1 or Another No:quot; ;<br />cin>>status;<br />}<br />void print()<br />{<br />if(status==1)<br />{<br />cout<<quot; Reg_no=quot; <<reg_no;<br />cout<<quot; Model=quot; <<model;<br />cout<<quot; No_wheel=quot; <<no_wheel;<br />cout<<quot; Fuel=quot; <<fuel;<br />}<br />else<br />{<br />cout<<quot; Reg_no=quot; <<reg_no;<br />cout<<quot; Model=quot; <<model;<br />cout<<quot; No_leaf=quot; <<no_leaf;<br />cout<<quot; Fuel=quot; <<fuel1;<br />}<br />}<br />};<br />void main()<br />{<br />clrscr();<br />road_water_vehicle rw;<br />rw.read3();<br />rw.print();<br />getch();<br />}<br />ABSTRACT CLASSES:<br />An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class (to be inherited by other classes). It is a design concept in program development and provides a base upon which other classes may be built.<br />A class is said to be abstrat class if it satisfies the following conditions.<br />1. It should act as base class.<br />2. It should not be used to create any objects.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class student<br />{<br />private:<br />int reg_no;<br />protected:<br />char name[20];<br />public:<br />void read()<br />{<br />cout<<quot; Enter the reg_no:quot; ;<br />cin>>reg_no;<br />cout<<quot; Enter the name:quot; ;<br />cin>>name;<br />}<br />void print()<br />{<br />cout<<quot; Reg_no=quot; <<reg_no;<br />}<br />};<br />class computer:public student<br />{<br />private:<br />int roll_no;<br />public:<br />void read1()<br />{<br />read();<br />cout<<quot; Enter the roll_no:quot; ;<br />cin>>roll_no;<br />}<br />void print1()<br />{<br />cout<<quot; roll_no=quot; <<roll_no;<br />cout<<quot; Name=quot; <<name;<br />}<br />};<br />void main()<br />{<br />clrscr();<br />computer s1;<br />s1.read1();<br />s1.print();<br />s1.print1();<br />getch();<br />}<br />NESTING OF CLASSES ( CONTAINER CLASSES )<br />class student<br />{<br />private:<br />char school_name[20];<br />char degree[20];<br />public:<br />void get_student_detail()<br />{<br />cout<<quot; Enter the name of school:quot; ;<br />cin>>school_name;<br />cout<<quot; Enter the higher degree earned quot; ;<br />cout<<quot; (Bachelor, Master, ph.d, Highschool ect.,)quot; ;<br />cin>>degree;<br />}<br />void print_detail()<br />{<br />cout<<quot; School or University:quot; <<school_name;<br />cout<<quot; Higher degree earned:quot; <<degree;<br />}<br />};<br />class employee<br />{<br />private:<br />char name[80];<br />int empno;<br />public:<br />void get_emp_detail()<br />{<br />cout<<quot; Enter Emoloyee No:quot; ;<br />cin>>empno;<br />cout<<quot; Enter employee name:quot; ;<br />cin>>name;<br />}<br />void print_emp_detail()<br />{<br />cout<<quot; Employee number:quot; <<empno<<quot; quot; ;<br />cout<<quot; Employee name:quot; <<name<<quot; quot; ;<br />}<br />};<br />class administrative<br />{<br />private:<br />char designation[80];<br />float basic;<br />employee emp;<br />student stud;<br />public:<br />void get_data()<br />{<br />emp.get_emp_detail();<br />cout<<quot; Enter designation:quot; ;<br />cin>>designation;<br />cout<<quot; Enter the basic pay:quot; ;<br />cin>>basic;<br />stud.get_student_detail();<br />}<br />void print()<br />{<br />emp.print_emp_detail();<br />cout<<quot; Designation:quot; <<designation<<quot; quot; ;<br />cout<<quot; Basic pay:quot; <<basic<<quot; quot; ;<br />stud.print_detail();<br />}<br />};<br />void main()<br />{<br />administrative a;<br />clrscr();<br />cout<<quot; Enter the details of administrative staffquot; ;<br />a.get_data();<br />cout<<quot; The detailsj of the staff are:quot; ;<br />a.print();<br />getch();<br />}<br />Pointer Expressions:<br />A Pointer is a variable data type and hence the general rule to assign its value to the pointer is same as that of any other variable data type.<br />For example:<br />int x;<br />int *ptr;<br />ptr = &x;<br />The memory address of variable quot; X quot; is assigned to the pointer variable ptr1.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />void main ( )<br />{<br />int x;<br />int *ptr;<br />x = 10;<br />ptr = &x;<br />clrscr ( );<br />cout<<quot; X=quot; <<x<<quot; and ptr =quot; <<ptr<<endl;<br />cout<<quot; X=quot; <<x<<quot; and *ptr =quot; <<*ptr<<endl;<br />getch ( );<br />}<br />The Output:<br />X = 10 and ptr = 0x24ccfff4<br />X = 10 and *ptr = 10<br />POINTERS TO OBJECTS:<br />Object pointers are useful in creating objects at run time. We can also use an object pointer to access the public members of an objects. The objects for a class can be accessed using pointers.<br />The general form for declaring object using pointer is:<br />Class classneme<br />{<br />member variable declaration;<br />member function declaration and definition;<br />};<br />classname object1;<br />classname *ptr;<br />ptr = & object1;<br />Ex:<br />#include<iostream.h><br />#include<conio.h><br />class computer<br />{<br />private:<br />int ram;<br />int speed;<br />char name[10];<br />public:<br />void read()<br />{<br />cout<<quot; Enter the value of ram, spesed, name:quot; <<endl;<br />cin>>ram>>speed>>name;<br />}<br />void print()<br />{<br />cout<<quot; Name=quot; <<name;<br />cout<<quot; Speed=quot; <<speed<<quot; MHzquot; ;<br />cout<<quot; Ram=quot; <<ram<<quot; MBquot; ;<br />}<br />};<br />void main()<br />{<br />computer c1;<br />computer *ptr;<br />ptr = &c1;<br />clrscr();<br />ptr->read();<br />ptr->print();<br />getch();<br />}<br />VIRTUAL FUNCTION:<br />Virtual function is a function which does not really exist. But appears real to some parts of a program.<br />It is used to call a particular function from derived class having more than one function with same name. For this the base class member function must be declared as virtual.<br />This is an application of polymorphism. Here, we use the pointer to base class to refer all the derived objects.<br />Example:<br />#include<iostream.h><br />#include<conio.h><br />class base<br />{<br />public:<br />void display()<br />{<br />cout<<quot; Display Basequot; ;<br />}<br />virtual void show()<br />{<br />cout<<quot; Show Basequot; ;<br />}<br />};<br />class derived : public base<br />{<br />public:<br />void display()<br />{<br />cout<<quot; Display Derivedquot; ;<br />}<br />void show()<br />{<br />cout<<quot; Show Derivedquot; ;<br />}<br />};<br />int main()<br />{<br />base b;<br />derived d;<br />base *bptr;<br />cout<<quot; bptr points to base quot; ;<br />bptr = &b;<br />bptr -> display();<br />bptr -> show();<br />cout<<quot; bptr points to Derived quot; ;<br />bptr = &d;<br />bptr -> display();<br />bptr -> show();<br />return 0;<br />}<br />Unformatted I/O<br />( i ) Input Operation:-<br />The input operations are carried out using the following Member function.<br />1. Overload Operator >><br />This operator is overloaded in the istream class and is used to give input from keyboard to program. The general form is<br />Cin>>var1>>var2>>........>>varn;<br />Ex:<br />int number;<br />float average;<br />cin>>number>>average;<br />2. get ( )<br />This function is used to input a single character to the program. This is a member function of istream class and is accessed with the help of the object cin. There are two types of get ( ) function.<br />a.get ( char )<br />b.get ( void )<br />get ( char ):<br />This function is used to assign the input character to its argument. The general form is<br />Syntax:cin.get ( variable-name );<br />Ex:char opt;<br />cin.get ( opt );<br />when cin.get ( opt ) is executed the given character input is assigned to the variable opt.<br />get ( void ):<br />This function is used to assign the input character to the variable on the left hand side. The general form is<br />Syntax:variable-name = cin.get ( )<br />Ex:char opt;<br />opt = cin.get ( )<br />when opt = cin.get ( ) is executed the given character is assigned to the variable opt on the left hand side.<br />3. getline ( )<br />This function is used to input a line of text to the program. This is a member function of istream class and is accessed with the help of the object cin. The general form is<br />Syntax:cin.getline ( variable-name,size);<br />Ex:char name[10];<br />cin.getline(name,10);<br />When cin.getline ( name, 10 ) is executed the given line of text size 9 is assigned to the variable name. One place is used for the terminator ‘’. The reading of the line ends when the new line character ( ) is reached or the size is reached.<br />( ii ) Output Operation:-<br />The output operation is carried out using the following member functions.<br />1. Overloaded Operator <<<br />This operator is overloaded in the ostream class and is used to output data from program to VDU. The general formis<br />cout<<outputlist;<br />Ex:int x = 10;<br />Float y = x + 20;<br />cout<<x<<y;<br />2. put ( )<br />This function is used to output a single character from program to the VDU. This is a member function of ostream class and is accessed with the help of the object cout. The general form is<br />cout.put ( variable-name );<br />Ex:char d = ‘a’;<br />…………..<br />cout.put ( d );<br />When cout.put ( d ) is executed this displays the character a on the VDU.<br />cout.put ( 35 );<br />This display the character # on the VDU. Because 35 is the ASCII value of hash (#)<br />3. write ( )<br />This function is used to output a line of textfrom program to VDU. This is a member function of ostream class and is accessed with the help of the object cout. The general form is<br />cout.write ( variable-name, size );<br />Ex:char name [10];<br />cout.write (name,10);<br />When cout.write(name,10) is executed the characters stored in the variable name is displayed on the VDU. If the size is grater than the length of the line then it displays beyond bounds of line.<br />Formatted console I/O:<br /> In c++ number of functions are available to format the output. These functions are the member functions of the class ios. They are<br />1.width ( )<br />2.precision ( )<br />3.fill ( )<br />4.setf ( )<br />5.unsetf ( )<br />1. width ( ):<br />This function is used to specify the width of the data to be printed. Since it is a member function, this is accessed with the help of the object cout. The general form is<br />Syntax:cout.width ( size );<br />25<br />Ex1:int a = 25;output:<br />cout.width ( 4 );<br />cout<<a;<br />Ex2:int a = 400;output:<br />400<br />cout.width ( 2 );<br />cout<<a;<br />Rules:<br />1.The value printed is right justified.<br />2.If the size of the width is less than the actual data width c++ automatically increases the size depending on the data size.<br />3.The include file iostream.h should be included in the program.<br />2. precision ( )<br />This function is used to specify the number of digits to be displayed after the decimal point. Since it is a member function this is accessed with the help of the object cout. The general form is<br />Syntax:cout.precision ( size );<br />Ex:float a = 4522.72597601;<br />cout<<a;<br />4522.7259<br />cout.precision ( 4 );output:<br />cout<<a;<br />4522.725976<br />cout.precision ( 7 );output:<br />cout<<a;<br />Rules:<br />1.Default precision size is 6.<br />2.default precision can be changed by giving our own precision size.<br />3.it won’t print the trailing zeros.<br />3. fill ( )<br />This function is used to print the given character in the unfilled position of the number printed. Since it is a member function, this is accessed with the help of the object cout. This is called filling and padding.<br />Syntax:cout.fill ( arg );<br />Ex:int a = 750;<br />***750<br />cout.fill (‘*’);output:<br />cout.width(6);<br />Rules:<br />1.Default fill is blank space.<br />2.If one character is used in the fill, it remains until we change it.<br />4. setf ( ) – Using bit field group<br />This function is used to set the flags and bit field group of the class ios. This is used to change the existing output settings. This is a member function of class ios.<br />Syntax:cout.setf(var1,var2);<br />Ex:int a = -46;<br />cout.fill(‘@’);output:<br />-@@@46<br />cout.setf ( ios::internal, ios::adjustifield );<br />cout.width ( 6 );<br />cout<<a;<br />Rules:<br />1.It is used to print the text left justified.<br />2.Numeric is printed right justified.<br />The tables given below lists the possible flag setting and its group.<br />Format requiredFlag (arg1)Bit-field (arg2)<br />Left-justified outputios :: leftios :: adjustfield<br />Right-justified outputios :: rightios :: adjustfield<br />Padding after sign ios :: internalios :: adjustfield<br />Or base indicator<br />(like +##20)<br />Scientific notationios :: scientificios :: floatfield<br />Fixed point notationios :: fixedios :: floatfield<br />Decimal baseios :: decios :: basefield<br />Octal baseios :: octios :: basefield<br />Hexadecimal baseios :: hexios :: basefield<br />setf ( ) – Without bit field group<br />This function is used to set the flags in the class ios. This is used to display the trailing zeros and the plus sign in the output. This is a member function of a class ios.<br />Syntax:cout.setf(flag);<br />Ex:float a = -100.000;<br />float b = 12.72;<br />cout.setf(ios :: showpoint);<br />cout.setf(ios :: showpos);<br />cout.width(10);output<br />-100.000<br />cout<<endl<<a;<br />cout.width(10);output<br />+12.72<br />cout<<endl<<b;<br />5. unsetf ( )<br />This function is used to clear the flags already set to their default settings.<br />FlagMeaning<br />ios :: showpointTo show trailing decimal point and zeroes<br />ios :: showposTo print + before positive numbers.<br />Managing Output with manipulators:<br />The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats. They provide the same features as that of the ios member functions and flags. They are<br />1.setw()<br />2.setprecision()<br />3.setfill()<br />4.flush<br />5.endl<br />1. setw()<br />This function is used to specify the width of data to be printed. This is equivalent to width().<br />Syntax:setw(size);<br />Ex:int a = 123;output:<br />123<br />cout<<setw(5)<<a;<br />2. setprecision()<br />This function is used to specify the number jof places after the decimal point. This is equivalent to precision().<br />Syntax:setprecision(size);<br />Ex:float y = 9873.273;<br />cout<<setw(10);output:<br />9873.27<br />cout<<setprecision(2)<<y;<br />3. setfill()<br />This function is used to print the given character jin the unfilled position jof the number printed. This is equivalent to fill().<br />Syntax:setfill(int var1);orsetfill(char var2);<br />Ex:int a = 567;<br />cout<<setfill(‘*’);output:<br />**567<br />cout<<setw(5)<<a;<br />int a = 567;<br />cout<<setfill(35);output:<br />##567<br />cout<<setw(5)<<a;<br />Where 35 is the ASCII value of the character #.<br />4. flush<br />This function is used to clear the output buffer.<br />Syntax:flush;<br />Ex:int a = 20;<br />cout<<a;<br />cout<<flush;<br />5. endl<br />This function brings the control to the new line and flushes the buffer while printing the data.<br />Syntax:endl;<br />Ex:int a = 10;<br />float b = 20.35;<br />cout<<a<<endl<<b;<br />Function for manipulation of File Pointers:<br />Normally the file pointer position is changed automatically during file processing. But we can control the file pointer movement with the help of the following member function. <br />1.seekg() – Moves get pointer (input) to a specified location.<br />2.seekp() – Moves put pointer (output) to a specified location.<br />3.tellg() – Gives the current position of the get pointer.<br />4.tellp() – Gives the current position of the put pointer.<br />The table given below shows the possible flags available:<br />FlagMeaning<br />ios :: begsets the pointer at the beginning of the file<br />ios :: cursets the pointer in the current position<br />ios :: endsets the pointer at the end of the file<br />1. seekg():<br />This function is used to move the input pointer to the position specified. The general form is<br />Syntax:filename-object . seekg (pos,flag);<br />Ex:fstream student;<br />student . open (“student.dat”, ios :: in);<br />student . seekg (-20, ios :: end);<br />When the last statement is executed, output file pointer is set at the end of the file and moves 20 byte backward from the end.<br />2. seekp():<br />This function is used to move the output pointer to the position specified. The general form is<br />Syntax:filename-object . seekp (pos, flag);<br />Ex:fstream student;<br />student . open (“student.dat”, ios :: out);<br />student . seekg (-20, ios :: end);<br />When the last statement is executed, output file pointer is set at the end of the file and moves 20 byte backward from the end.<br />3. tellg()<br />This function returns the byte number of the current position of the input file pointer. The general form is<br />Syntax:int var1 = filename-object . tellg();<br />Ex:fstream student;<br />student . open (“student . dat”, ios :: in);<br />student . seekg(20);<br />int p = student . tellg();<br />When the above statements are executed 20 is assigned to p.<br />4. tellp()<br />This function returns the byte number of the current position of the output pointer. The general form is<br />Syntax:int var1 = filename-object . tellp();<br />Ex:fstream student;<br />student . open (“student . dat”, ios :: out);<br />student . seekg(20);<br />int p = student . tellg();<br />When the above statements are executed 20 is assigned to p.<br />