


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Introduction of C++ After the invension of c, it worked as a most popular structured programming language, which have multiple features used inside different and complex structure of other program but the concept of C was in procedure oriented, which means combination of multiple programs but the concept of variables in C represent their global declearation value where all programs can easily share their Tvariables according to their necessity. But this feature to thread the security of programs and user can change the value of variable from any portion of program. So, it is necessary for programmer to introduce a concept where all variables are combined together and only function can be used according to the feature or declearation of crods. It is necessary too bind all the variables in a group and this concept is known as “Class”, where all function made publically open (called- Encapsulation). For increase the above feature Bjarne staroustrop, who was the developer of Simula 67 both. Simula 67 was the language who used the concept of Class and Variables are bind together but it was a object based language (not object oriented). In 1980, Bjarne staroustrop introduces a language, which created by the concept of Algol 68, Simula 67 and C. when it was developed, it introduced as “c with class”. In 1983, Risc Mascitte renamed as C++, after using the concept and format of increment operator ++. Introduction of Object Oriented language Object is the physical existance throughout all parts are instantiate together called Object. A group of information where logical framnework are used to combine the instance variable.
functions logical frame work parts Object oriented language made by the combination of Object and Class both, where Class is the logical frame work of all variables or parts which are instantiate together to create a function. We can say, Class is a blueprint or combination made by the behaviour of variable and function both, where an object is the physical existance of any Class through the output or functioon can be determine with the allocated memory area. For example- If a car is an object then its parts and function which are logically added inside it, is caalled Class. when multiple parts and functions are combined through a logical frame work, called – Class and exist by Object. Key concept of Object Oriented Language/ Features of c++ a) Class b) Object c) Method d) Data abstraction e) Encapsulation f) Data binding g) Message passing h) Reusability/ Inheritance i) Polymorphism j) Contructor k) Deligation l) Genericity Explanation- a) Class- A logical frame work of all parts and functions through informations are present in a group and no any individual informations can display without the permission of class. We can say, Class is a group of object which has identical properties to make or create a common behaviour of different functions. It allows the variables to work in private format and doesn’t move independently from the outside of function. Class can also allow the functions can make publically open.
parent child Class A Class B Class C Class A Class B Class C Class D which inherited by their sub class is called, Base class in C++ and Super class in Java. In the class which inheriting the properties of their Base class is called Derived class and Sub class in Java. In Single Inheritance, the base and super class is inherited by their sub class but sub class have no authority to share their properties with super class. Base class Devived class II. Multilevel Inheritance- When the super class share their properties with sub class and that sub class share their properties with other class, is called Multilevel Inheritance. In this Inheritance, the sub class behave like an intermediary, is called Intermediate class. For ex- A relation of grand parent, parent and child is defined as Multilevel Inheritance feature. Base class Intermediary class Sub/derived class In this Inheritance, a class is behave like a sub class for their super class and super class for their sub class. III. Hierarchical Inheritance- When a single super class or base class inherited by different sub class, when a no of subclass is more than 1, is called Heirarchical Inheritance.
Class A Class B Class C Class D Class A Class B (^) Class C Class D Class E Class A Class D Class B Class C IV. Multiple Inheritance- The feature of Multiple Inheritance is only supported in C++, where more than one super class are inherited or executed by a single sub class. V. Hybrid Inheritance- The feature of Inheritance is only used in C++ because of Multiple Inheritance facility, or we can say, Hybrid Inheritance is created by the combination of Single and Multiple Inheritance both. So, it doesn’t used in Java. Single Inheritance VI. Multipath Inheritance- When a base class is inherited by two more classes, where these two derived classes are inherited by other derive class, called Multipath Inheritance. We can say, it combines the feature of Multiple Inheritance, Hierarchical inheritance as well as Multilevel Inheritance.
Class A { }; Class B { }; Class A { }; Class B { }; Class C{ A.a; B.b; }; without value in C++, it find the garbage. So, in compare of Java, it is necessary to declare the Default constructor in C++ to ignore the garbage. k) Delegation- When we talk about Inheritance, it consist “Kind of relationship” between two classes, where class one can be defined by other class, But when two different class can share their method and worked as a data member for other class is define the “Has a relationship” and this type of feature is called Delegation. Kind of relationship Inheritance Has a relationship Delegation l) Generosity- In C++, programmer creates the template to identify the data type where these data type are run or execute by compiler automatically. In this kind of template, functions are easily identified by any data type, is called Generosity.
Input/output Stream in C++ The I/O system in C++ is designed to work a wide verity of device including terminal, disk & tape drive. All devices are different from other and I/O system supplies an interface to the programmer and actual device which define above. This interface is known as Stream. A stream is a sequence of byte and act as a source in which input Data is obtained (called input Stream) and as a destination in which output data can be sent (called output Stream). Input Stream Output Stream C++ contains several predefined stream that are automatically opened when a program begins its execution. “cin” and “cout” also included in it where cin represent the input stream and cout represent the output stream. Hierarchy of Stream classes:- C++ I/O stream support the hierarchy of classes which is used to manipulate both the console input/output and storage information is called as stream class. We can say stream classes are used with console device. Note:- Console is a default input/output device used for appropriate input/output operation. Class istream and ostream is derived from base class ios where ios contain member variable object StreamBuf to places the buffer. The class iostream is derived from the class istream and ostream by using multiple inheritances. Input Device Program Output Device IOS IStream OStream^ StreamBuf Istream_with assign (^) IOStream File Buffer OStream_with assign IOStream_with assign
Likewise any other data type object may be used to the function argument by three different method They are:-
#include<conio.h> class add{ private: int a,b; public: void input(int x,int y) { a=x; b=y; } void sum(add m,add n) { a=m.a+n.a; b=m.b+n.b; } void display() { cout<<"\n add="<<a+b; } }; void main() { add A; A.input(84,54); A.display(); add A1; A1.input(4,5); A1.display(); add A2; A.sum(A1,A2); A.display(); getch(); } 3) Pass-By-Address:- An object is passed by address to call the function and the process involved is called as call-by- address. We can say in other word address of object is explicitly sent to a function. // C++ program for Pass-By-Address #include<iostream.h> #include<conio.h>
cin>>nm; cout<<"\n enter age:"; cin>>age; } void putdata() { cout<<"\n student name:"<<nm; cout<<"\n student age:"<<age; } }; void main() { int i; student s1[3];//array declared in object cout<<"\n enter the detail of student:\n"; for(i=0;i<2;i++) { s1[i].getdata(); } cout<<"\n Detail of student:"; for(i=0;i<2;i++) { s1[i].putdata(); } getch(); } Pointer to Object:- When we access the object by pointer is called pointer to object. // C++ program for pointer to object #include<iostream.h> #include<conio.h> class distance { int feet; float inch; public: void getdetail() { cout<<"\n enter feet:";
cin>>feet; cout<<"\n enter inch.:"; cin>>inch; } void display(){ cout<<"\n value of feet :"<<feet; cout<<"\n value ofinch :"<<inch;} }; void main(){ distance d; distance ptr=&d; (ptr).getdetail(); (*Ptr).display(); getch(); } Friend Function:- C ++ allow a mechanism through which non member function has access permission to the private member of class called as friend function. Friend is a keyword and it called directly without using object. But it can’t be accessed the member directly and always used an object as well as dot operator to access the member variable of class ( ie private/public) The main demerit of friend function is that it tempered the rule of encapsulation and data hiding //C++ program for friend function #include<iostream.h> #include<conio.h> class sum { private: int a,b; public: void read() { cout<<"enter no"; cin>>a; cout<<"enter no"; cin>>b; }
a=m; b=n; c=o;} num(int m,int n){ cout<<"\n constructor called with different parameter:"; a=m; b=n;} void show(){ cout<<"\naddition of three parameter:"<<a+b+c; cout<<"addition of two parameter:"<<a+b;} }; void main() { num N(4,5,8); N.show(); num N1(4,76); N1.show(); getch(); } Destructors:- We know that constructor is a special method/member function which called when an object is created. It decides how the objects of class are created, initialized and copied. But Destructor work just opposite from constructor. It destroys an object and executes at the end of function when an object is not in use. Note:- Destructor is proceeded by tild(~) operator. //c++ program for Destructor:- #include<iostream.h> #include<conio.h> class Byte { int bit; int byte; public: byte() { cout<<”constructor called”; bit=64;
byte=bit/8; } ~byte(){ cout<<”\n Destructor called”; cout<<”\n bit=”<<bit; cout<<”\n byte=”<<byte;} }; void main() { clrscr(); byte x; //constructor call automatically byte(); //calling constructor manually x.byte::~byte(); getch(); } Explanation:- Class byte has an object x use to automatically invoke the constructor and when we use byte() That means constructor call manually. But for call destructor we must use class with object, only object can’t be able to call destructor .so we use a class name with object to call destructor by constructor after declared it inside the statement of constructor. Inheritance:- All object and object oriented language must hold/process the feature of reusability i.e reusable facility which means “Code Once Defined is Reusable”. To do the needful i.e to support the feature of reusability an object oriented language i.e C++ provides a technique a technique/way/mechanism to inherit the existing class/type in a new class/type. The class/type that is inherited is called base class (i.e super class in Java) and the class/type that does inheriting is called derived class (i.e sub class in Java) and the process/way/mechanism involved into it is called inheritance. The derived class may inherits all the property of its base class and may consists its own characteristics. Types of Inheritance supported in C++:- C++ has different inheritance facility. They are:-
cout<<"\n Name:"<<nm<<"\n age:"<<age; cout<<\n Height:<<height<<"\n Weight:"<<weight; } }; void main() { clrscr(); Derived D; D.getdata(); D.show(); getch(); } Multilevel Inheritance:- When a derive class become a base/super class for another derive class is called multilevel inheritance. (Base Class) (Intermediate Class) (Derived Class) //C++ Program for multilevel Inheritance Grand Father Father Children Syntax:- class <class_name>:
#include<iostream.h> #include<conio.h> class student { int roll; public: void getdata(int x) { roll= x; } void putdata() { cout<<"\nRoll:"<<roll; } }; class test:public student { protected: int sub1,sub2; public: void getmarks(int s1, int s2){ sub1=s1; sub2=s2; } void putmarks(){ cout<<"\nsub1=:"<<sub1; cout<<"\nsub2=:"<<sub2; } }; class result:public test{ float total; public: void display() { total=sub1+sub2; putdata(); putmarks(); cout<<"\ntotal:"<<total; } };