Note:- Required header file for setfill, precision and setbase.
#include ➢ Memory Allocation Operator:- • new • delete in C :- malloc()
int *p; p=(type cast)malloc(sizeof(data_type)); p=(int *)malloc(sizeof(data_type)) *p=10;
free(p);
In C++:- new p=new int; p=new int(10); static/dynamic memory allocation operator.
delete p; ➢ new and delete :- new and delete are two memory management unary operators in C++ that perform the task of a locating and releasing the memory. The new operator can be used to create object of any type. If sufficient memory is not available forallocating the new operator return a null pointer.
Syntax:-
Pointer variable=new datatype
For ex:- int *p;
P=new int;
Pointer variable=new datatype[ ] //for array
Int *p=new int[10];
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 7
Or
Int b=10;
Int *p=new int[b]; //in dynamic variable are used
We can also initialize the memory using the new operator at the time of declaration.
Syntax:-
Pointer variable=new datatype(value);
For ex:-
Int *p=new int(10);
❖ The new operator offers the following advantage over the function of malloc :- (i) The new operator automatically computes the size of the data object we need not use the operator sizeof. (ii) It automatically return the correct pointer type so there is no need to use a type cast. (iii) It is possible to initialize the object while creating the memory space. (iv) Like any other operator new can be overloaded.
❖ delete:- The delete operator can be used to release the memory space for re-use. Syntax:- delete pointer variable; ex:- delete p; ➢ Function in C++:- (i) Inline function (ii) Default argument function (iii) Function overloading (Polymorphism)
❖ Function Declaration for function overloading:-
Intsum(int,int); //function declaration double sum(double,double); //function declaration char sum(char,char); //function declaration
1. Inline function:- These are those function whose function body is inserted in place of the function call during the compilation process. Thus there is no transfer of control between the calls and the called function which returns improves the program execution time. The functions are made inline when they are small enough to be defined one or two lines to make a function inline prefix the keyword ‘inline’ to the function definition all inline function must be defined before they are called.
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 8
Inline function containing large lines of codes still provides execution time benefits but due to huge wastage of memory of the program, these execution time benefits small. In such cases use of normal function is recommended. ❖ Some of the situation where inline expansion may not work are:- (i) For function returning values if a loop, switch or a goto statement exists. (ii) If functions contain static variable. (iii) If inline function are recursive.
2. Default argument functions:-A function without specifying all its argument called a default argument function. In such type of function the function assign a default value to the parameter in function declaration which does not have a matching argument. Only the trailing argument can have default value and therefore we must add default from right to left. We can’t provide a default value to the particular argument in the argument list. Default arguments are useful in situation where some arguments always have the same value. 3. Function Overloading:-It is the one of the most exciting feature of C++. In C++ two or more than two functions can share the same name that perform similar operations but have different parameter list. In such situations the function that share the same name are said to be overloaded and the process is called function overloading.
Using the concept of function overloading we can design a family of function which are function name same but with different argument list.
❖ If the function differ only in the return time they are not considered overloaded function and hence the compiler generate an error message this is become it is not possible for the compiler to determine which version of the function to case. ❖ Advantage of the function overloading:- (i) We need remember single name instead of multiple names of the function that perform similar type of operation. (ii) Overloaded functions that perform similar task that makes complex program more readable and understandable. (iii) Maintainability of the program becomes easier. (iv) Overloaded functions are used for handling class.
➢ Type conversion:- • Implicit or automatic type conversion:-
The conversion of a data type which is done automatically by the compiler is called implicit type conversion.
When two variable of difference data types are used in same expression the C++ compiler uses built in library function to transfer the variable to common data type before evaluating the expression. To decide which variables data type is to be converted compiler uses the hierarchy of data type and converts the lower class type to that of a higher data type.
• Hierarchy of data type:- • Implicitly types:- long double>double>float>long int>int>char
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 9
• Explicitly type conversion(type conversion):-
In addition to the implicitly type conversion C++ also provide explicit type conversion within expression by the programmer using the cast operator.
Using cast operator we can overwrite C++ default type conversion by explicitly specifying our own temporary data type according to the requirements.
Syntax:-
data_type(operand)or(data_type)operand
ex:- float(a);
(float)a;
******
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 10
➢ Class:-A class is a way to find the data and its associated functions together. It allows data and function to be hidden if necessary. A class has two parts:- (i) Class declaration (ii) Class function definition
The class declaration describes the type and scope of its data member and member functions. The class function definition describes how the class functions are implemented. The variable declared inside the class are known as data member and the functions are known as member function.
The class members that have been declared as private can be access only from within class and the public member can be access from outside the class also. The use of the keyword ‘private’ is optional, by default the member of a class are ‘private’. The data hiding using private declaration is the key feature of object oriented programming.
❖ Characteristics of member function:- (i) Different classes can use the same function name. The membership level will resolve their scope. (ii) Member function can access the data of this class. A non-member function can’t do so. However an exception to this rule is a friend function. (iii) A member function can call another member function directly without using dot operator.
➢ Static data member:-As we know that each object of a class holds its separate copies of all the data member of the class which are not shared by different object of same class however in certain situation it may be required to share common copy of the data member across all the object of the same class. This can be done by using static data member.
A static data member is the data member that gets memory only once for the whole class. This common copy is shared by all objects of its class. The static data member is declared within the class by prefixing the static keyword and defined outside the class using scope resolution with class name.
Syntax:-
Class class_name
{
………….
…………
Static data_typedata_member_name;
…………
};
Data_typeclass_name::data_member_name=initial_value;
Example:- S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 11
Class Account
{
…………
…………
Static double rate;
…………..
}
Double Account::rate=12.96; ❖ Properties of static data member:- (i) The static data member gets memory only one which is share by its entire object. (ii) The static data member are associated with the class and not with any object so they are also called as a class variable. (iii) The static data member must be defined after the class. (iv) The static data member follows the public, private and protected access roots. (v) The static data member exists before any object of a class is created. ➢ Static member function:- The static member function is similar to normal member function of a class but the only difference is that it can access only static member (data or function) declare in the same class. In other words we cannot access non static member inside the definition of in static function. The declaration of static member function is same as that of non-static function except that the function declaration in the class body must be presided with the keyword static and the function definition that appears outside the class body must not be specified with the static keyword. If the member function is defined inside the class the function header () must be presided by static keyword. Static member function can be called using the class name instead of its object.
Syntax:-
Class class_name
{ ………..
Private:
…………
Public: staticreturn_typefunction_name(argument); //declared inside the class
}
****** classclass_name
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 12
{
Private:
…………
Public:
staticreturn_typefunction_name(argument) //defined inside the class
{
………
}
};
Ex:- class account
{
…….
Public:
………
static void updaterate(double x)
{
rate=x;
}
};
➢ Const member function:- When a member function doesn’t modified any data member of its class and simply access it then such a member function are declared as const member function. T make a member function const defined inside the class as const. place the ‘const’ keyword after function parameter list and before the opening curly braces. All over if the member function is defined outside the class then ‘const’ keyword must be specified both in its declaration and as well as its definition. We can’t declare a static member function as const.
Syntax:- Class ABC { ………. Public: ………..
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 13
void function(parameter)const; //const member function declaration }; void ABC::function_name(parameter)const //const member function definition { ……… } ➢ Private member function:-Usually member function are public but in some cases it is useful to declare one or more function to be private. The private member function can only be called through another member function of the same class by simply referring its name without dot (.) operator. The private member functions are normally used to hide certain operations of the class from outsider and serve as utility function to other function to the class. For example calculating gross salary of an employee the function. We should be hidden from outside the class. No use any keyword only declared and defines in private section. ➢ Inline member function outside the class:-We know that if a member function is defined inside a class body the compiler consider it as a inline function and if the member function is defined outside the class body then it is not an inline function. However the member function defined outside its class body can be made inline by explicitly using the keyword ‘inline’ prefix on the function definition. ➢ Friend function:- A friend function is a non-member function that has been granted access to all the member of a class. To make a function friend of a class declare the function within the class by prefixing its declaration with keyword ‘friend’. A function which is declared as a friend of a class will be able to access private data of that class. The declaration of friend function can be placed anywhere in the class. The definition of friend function is specified outside the class body. The function does not use either the keyword ‘friend’ or the scope resolution. ❖ Characteristics of friend function:- (i) It is not a member function of the class. But it as full access authority to the private member of the class. (ii) It can’t be caused using the object of that class. (iii) It can be invoked (called) like a normal function without help of any object. (iv) It can be declared either in the public or private section of a class. (v) Usually it has the object as argument. (vi) It acts as a bridge between two classes. (vii) A friend function can be used to overload operator.
❖ A function friendly to two classes:- • In a situation where we want to a function to access the private data member of two or more different classes. In order to solve this problem we can define this function as a friend of two or more different classes and hence in this sense a friend function acts as a bridge between two or more classes.
➢ Constructor: -A constructor is a special member function which is use to initialize the object of its class. It is special because its name is same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because its construct the value of data member of the class. ❖ Characteristics of constructor:- (i) They should be declared in the public section. (ii) They don’t have return type not even void. S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 14
(iii) They can’t return values. (iv) They are invoked automatically when the objects are created. (v) Like other C++ functions they can have default arguments. (vi) They can’t be inherited. ❖ Types of Constructor:- (i) Default Constructor:- A constructor that accepts no argument is called the default constructor. (ii) Parameterized Constructor:-The constructor that take argument are called parameterized constructor. The parameterized constructor is used when it is necessary to initialize various data element of different object with different values. Parameterized constructor may be overloaded.
When a constructor has been parameterized we must pass the initial values as arguments to the constructor when an object is created. They are two way to invoke the parameterized constructor.
(a) Implicitly:- Syntax:- Classnameobject(parameter_list); (b) Explicitly:- Syntax:- Classname object; Object=classname(argument); • Destructor: - A destructor is a member function having a same name as that of constructor but it is presided by a tiled (~) sign. A destructor is used to destroy the object that are already created by the constructor. A destructor never take argument non return any value. It will be invoked by the compiler upon exit from the program or block or function to cleanup storage i.e. no longer accessible. A class can have only one destructor and hence it can’t be overloaded. • Copy Constructor:-a copy constructor is a constructor that creates a new object using an existing of the same class and initialize each data member of newly created object with corresponding data member of existing object passed as argument. Since it creates a copy of an existing object so it is called copy constructor. The object passed to the constructor should be passed by reference and not by a value. Syntax:-
Calling:-
Class_namenewobject(existing object);
Or
Class_namenewobject=existing_object);
Ex:- main()
{
ABC obj(10);
ABC obj2(obj); //or
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 15
ABC obj2=obj;
}
Definition of Copy Constructor:-
class_name
{
int a;
…………
public:
class_name(classname temp object)
{ a=temp.object.a;
}
};
➢ Operator overloading:- Operator overloading is main feature of C++. Operator overloading means to give an additional task to an operator we can overload the C++ operator except the following:- (i) Class member access operator (.,.*) (ii) Scope resolution (::) (iii) sizeof operator (iv) conditional operator
When an operator is overloaded its original meaning is not lost to define an additional task to an operator. We must specified what it means in relation to class to which the operator is applied. Operator is done with the help of a special function is called operator function must be either member function or a friend function.
Syntax:-
As a member function
Return_typeclass_name::operator(argument_list)
{
……….
}
Example:-
Class_name/void class_name::operator++()
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 16
{
………
}
Binary:-classnameclassname::operator+(classname with object)
Syntax:-
As a friend function
Return_typeoperator op(argument list)
{
…………….
}
For ex:-
Class_name operator+(argumentlist)
{
…………………
}
• Rules for operator overloading:- (i) Only existing operator can be overloaded. (ii) The overload operator must have at least operand. (iii) We can’t change the basic meaning of an operator. (iv) We can’t use friend functions to overload certain operator:- =, (), [], -> (v) Unary operators overloaded by member functions take no argument and return no values. But overloaded by a friend function take one reference argument. (vi) Binary operators overloaded by a member function take one argument and overloaded by a friend function take two argument. (vii) Binary arithmetic operator must return a values. (viii) When using binary operators overloaded by a member function the left hand operant must be an object of the related class. **********
➢ Inheritance:-
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 17
Inheritance is one of the key features of object oriented programming language. Inheritance is a process of creating a new class from an existing class. In C++ a new class can inherit the data member and member function of the existing class and can also add member of its own. This process of creating a new class from an existing class is known as inheritance. The existing class that is being inherited is called a base class or a parent class or a super class or an ancestor class. And the new class that is inherits the member and functionalities of existing class are called derived class or a child class or a sub-class or a descendent class. The base class is inherited through public, private and protected access specified control the access of inherited member of the base class in the derived class and other part of the program. If no access specifies is specified then it is private by default.
• Syntax for declaring a derived class:-
Class derived_classname:access_specifier base_class_name
{
…………………
}
Here derived class name is the name of derived derived class and base class name is the name of base class whose member are inherited by the derived class the column operator indicates that derived class name is derived from base class name. The access specifier indicates the scope of the member of this class.
• Public Inheritance:- Base Derived Inheritance private: private: private: base_pri; Public visibility of member of base der_pri; der_pri; public: Class in the object of public: public: base_pub; der_pub; der_pub; protected: derived class protected: base_pub; base_pro; der_pro; protected:
der_pro; base_pro;
• Protected Inheritance:- Base Derived Inheritance private: S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 18 private: private: base_pri; der_pri; der_pri; public: public: public: base_pub;
Protected visibility of member of base
Class in the object of
derived class
• Private Inheritance:- Base Derived Inheritance private: private: private: base_pri; Private visibility of member of base der_pri; der_pri; public: Class in the object of public: base_pro; base_pub; der_pub; base_pub; protected: derived class protected: public: base_pro; der_pro; der_pub;
protected:
der_pro;
• Types of inheritance: There are four types of inheritance:
(i) Single Inheritance (ii) Multi-level inheritance (iii) Multiple inheritance (iv) Hybrid inheritance
(i) Single Inheritance:- In single inheritanceBase one base class and one derived class.
Class
Derived Class S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 19
(ii) Multi- level Inheritance:- In multi-level inheritance base class having single child class and child class having single sub-child class so and so..
Base
Class
Derived
Class1
Derived
Class2
(iii) Multiple Inheritance:- In multiple inheritance, one child class having more than one base class.
Base Base cl;ass2 Class1
Derived
Class2
(iv) Hybrid Inheritance:- In hybrid inheritance, Combination of more than one inheritance.
➢ This pointer:-
When ever be call a member function in association with an object the address of the object is implicitly first argument which is received in the called member function using a special pointer known as this pointer. The this pointer contains
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 20
the address of the object that generates the call. It is implicitly defined in is member function and its data type is that f a pointer to the class type.
➢ Void pointer:-
A void pointer can hold the address of any type of variable. It is used when the abject type of variable is either unknown or change under particular circumstances. It is also known as generic pointer because it has ability to tag the address of variable of any data type.
Syntax:-
Void *ptr;
A void pointer contains the address of an unknown data type. The compiler has no information about the type. It is pointing to. Hence void pointer must be converted to a pointer of particular types. This can be done by explicitly casting to the required data type.
Syntax:- explicitly type cast
*((data_type *)generic pointer); ➢ Polymorphism:-
Polymorphism is a very powerful concept in C++. The word polymorphism is derived from two words “poly” which means many and “morphism” which means forms. So polymorphism means the ability to take many forms. Morphism can be defined as one interface multiple methods which means that one interface can be used to perform different but related activities.
The concept of function overloading and operator overloading implements polymorphism as they provide single interface for performing different but related task we can also take advantage of inheritance for implementing polymorphism (run time).
• Classification of Polymorphism:- (i) Compile time or static or Early binding polymorphism (ii) Run time or dynamic or late binding polymorphism
(i) Compile time polymorphism:- In compile time polymorphism static binding is performing. In static binding the compiler takes the design to select appropriate function to be called in response to function call at compile time. The compile time polymorphism is implemented in C++ using function overloading and operator overloading. In both cases the compiler has all the information about the data type and number of argument needed so t can select the appropriate function at the time of compilation.
(ii) Run time polymorphism:- In run time polymorphism dynamic binding is performed in dynamic binding the decision regarding the selection of appropriate function to be called is made by the compiler at run time and not at compile time. The advantage of dynamic binding is that it allows greater flexibility by enabling user to create class
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 21
libraries that can be re-used and extended as per requirement. It provides a common interface in the base class for performing multiple tasks whose implementation is present in derived class. In inheritance virtual function implements the run time polymorphism. ➢ Pure Virtual function:- The virtual function having an initialize (=0) in its declaration and which doesn’t have body ( { open and close) is known as pure virtual function. A pure virtual function is also known as a dummy function or a do-nothing function. Syntax:- Virtual return_type function_name()=0; ➢ Member differencing operator:- Si no. Operator Function 1 .* To access a member using object name and a pointer to that member 2 ::* To declare a pointer to a member of a class 3 ->* To access a member using a pointer to the object and a pointer to that member.
➢ Difference between pass by value and pass by reference:- Pass by Value Pass by reference When an argument is pass by value any When an argument is pass by reference any modification made in formal argument in the modification made in formal argument in the called function is not reflected back to the actual called function are reflected back to the actual argument in the calling function argument in the calling function Formal argument in the function definition in case Formal argument in function definition in case of of the call by value is declared as the call by reference is declared Fun_name(int i) Fun_name(int &i) { { ……… …….. } } Here the formal argument get separate memory Here the formal argument point to the actual and value of actual argument is assigned to the argument corresponding formal argument The argument passed by value may be a constant, The argument passed by reference must be a a variable or an expression variable This type of function can return only one value Passed by reference can return more than one using return statement value It is not suitable for passing argument of large size It suitable to passing argument of large size as as separate copy of its consumer a job of memory separate copy of its made in the called function space
➢ Exception Handling:- Exception handling is provide a way of transfer control and information to a unspecified called that has expressed function of a given type exception of ordinary type can be try, throw and catch and set of exception a function may throw can be specified.
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 22
Exception handling is run the mainly generate the run time error are as follows- • There are:- try :- try is basically back where exception is generated. throw:- When exception is generated it must be throw the exception throw is usually try back. catch:- catch() is also a block when exception handling statements are define it is basically a function & accept exception that is by throw.
*******
S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 23