0% found this document useful (0 votes)
4 views

CPP

CPP

Uploaded by

swapnil kayde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CPP

CPP

Uploaded by

swapnil kayde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

C++ Programming

Trainer : Rohan Paramane


Email: [email protected]

Sunbeam Infotech www.sunbeaminfo.com


Agenda

• Scope
• Macro
• Operator Overloading
• Exception Handling
• Association
• Composition
• Aggregation
• Inheritance
• Types of Inheritance

Sunbeam Infotech www.sunbeaminfo.com


Operator Overloading

• operator is token in C/C++.


• It is used to generate expression.
• operator is keyword in C++.
• Types of operator:
• Unary operator ( ++,--,&,!,~,sizeof())
• Binary Operator (Arithmetic, relational, logical , bitwise, assignment)
• Ternary operator (conditional)
• In C++, also we can not use operator with objects of user defined type directly.
• If we want to use operator with objects of user defined type then we should overload operator.
• To overload operator, we should define operator function.
• We can define operator function using 2 ways:
• Using member function
• Using non member function

Sunbeam Infotech www.sunbeaminfo.com


Need Of Operator Overloading

• we extend the meaning of the operator.


• If we want to use operator with the object of use defined type, then we need to overload operator.
• To overload operator, we need to define operator function.
• In C++, operator is a keyword
• Suppose we want to use plus(+) operator with objects then we need to define operator+( ) function.

We define operator function Point pt1(10,20), pt2(30,40 ), pt3;


either inside class (as a
member function) or pt3 = pt1 + pt2; //pt3 = pt1.operator+( pt2); //using member function
outside class (as a non- //or
member function). pt3 = pt1 + pt2; //pt3 = operator+( pt1, pt2); //using non member function

Sunbeam Infotech www.sunbeaminfo.com


Operator Overloading
using member function using non member function
• operator function must be member • Operator function must be global
function function
• If we want to overload, binary operator • If we want to overload binary operator
using member function then operator using non member function then operator
function should take only one function should take two parameters.
parameter. • Example : c3 = c1 + c2; //will be called as -
• Example : c3 = c1 + c2; //will be called as - -----c3 = operator+(c1,c2);
----- c3 = c1.operator+( c2 ) Example:
Example : Point operator+( Point &pt1, Point &pt2 ) //Non Member
Point operator+( Point &other ) //Member Function Function
{ {
Point temp; Point temp;
temp.xPos = this->xPos + other.xPos;
temp.xPos = pt1.xPos + pt2.xPos;
temp.yPos = this->yPos + other.yPos;
return temp; temp.yPos = pt1.yPos + pt2.yPos;
} return temp;
}

Sunbeam Infotech www.sunbeaminfo.com


We can not overloading following operator using member as well as non
member function:
1. dot/member selection operator( . )
2. Pointer to member selection operator(.*)
3. Scope resolution operator( :: )
4. Ternary/conditional operator( ? : )
5. sizeof() operator
6. typeid() operator
7. static_cast operator
8. dynamic_cast operator
9. const_cast operator
10. reinterpret_cast operator

Sunbeam Infotech www.sunbeaminfo.com


We can not overload following operators using non member function:

• Assignment operator( = )
• Subscript / Index operator( [] )
• Function Call operator[ () ]
• Arrow / Dereferencing operator( -> )

Sunbeam Infotech www.sunbeaminfo.com


Program Demo without operator overloading

• Create a class point, having two fileld, x, y.


• Point( void )
• Point( int x, int y )
int main( void )
{
Point pt1(10,20);
Point pt2(30,40);
Point pt3;
pt3 = pt1 + pt2; //Not OK( implicitly)
return 0;
}

Sunbeam Infotech www.sunbeaminfo.com


Exception Handling
• If we give wrong input to the application then it generates runtime error/exception.
• Exception is an object, which is used to send notification to the end user of the system if any exceptional situation occurs in the program.
• To handle exception then we should use 3 keywords:
• 1. try Note : For thrown exception, if we
• try is keyword in C++. do not provide matching catch block
• If we want to inspect exception then we should put statements inside try block/handler. then C++ runtime gives call to the
• Try block may have multiple catch block but it must have at least one catch block. std::terminate() function which
• 2. catch implicitly gives call to the std::abort()
• If we want to handle exception then we should use catch block/handler. function.
• Single try block may have multiple catch block.
• Catch block can handle exception thrown from try block only.
• A catch block, which can handle any type of exception is called generic catch block / catch-all handler.
• For each type of exception, we can write specific catch block or we can write single catch block which can handle all types of exception. A
catch block which can handle all type of exception is called generic catch block.
• 3. throw
• throw is keyword in C++.
• If we want to generate exception explicitly then we should use throw keyword.
• "throw statement" is a jump statement.
• To generate new exception, we should use throw keyword. Throw statement is jump statement.

Sunbeam Infotech www.sunbeaminfo.com


int num1;
• In C++, try, catch and throw
accept_record( num1 );
keyword is used to handle
int num2;
exception. accept_record( num1 );
try
{
if( num2 == 0 )
throw "/ by zero exception";
int result = num1 / num2;
print_record( result )
}
catch( const char *ex )
{ cout<<ex<<endl; }
catch(...)
{
cout<<"Genenric catch handler"<<endl;
}

Sunbeam Infotech www.sunbeaminfo.com


Consider the following code
In this code, int type exception is thrown int main( void )
{
but matching catch block is not available.
int num1;
Even generic catch block is also not
accept_record(num1);
available. Hence program will terminate. int num2;
Because , if we throw exception from try accept_record(num2);
block then catch block can handle it. But try
with the help of function we can throw
exception from outside of the try block. {
if( num2 == 0 )
throw 0;
int result = num1 / num2;
print_record(result);
}
catch( const char *ex )
{ cout<<ex<<endl; }
return 0;
}

Sunbeam Infotech www.sunbeaminfo.com


Consider the following code
If we are throwing exception from int calculate(int num1,int num2) throw(const char* )
{ if( num2 == 0 )
function, then implementer of function
throw "/ by zero exception";
should specify “exception specification
return num1 / num2;
list”. The exception specification list is }
used to specify type of exception int main( void )
function { int num1;
may throw. accept_record(num1);
If type of thrown exception is not int num2;

available in exception specification list accept_record(num2);


try
and if exception is raised then C++ do
{ int result = calculate(
execute catch block rather it invokes
num1, num2 );
std::unexpected() function. print_record(result);
}
catch( const char *ex )
{ cout<<ex<<endl; }
return 0; }

Sunbeam Infotech www.sunbeaminfo.com


Scope

• It decides area/region/boundry in which we can access the element.


• Types of scope in C++:
1. Block scope
2. Function scope
3. Prototype scope
4. Class scope
5. Namespace scope
6. File scope
7. Program scope

Sunbeam Infotech www.sunbeaminfo.com


Example Scope
int num6; //Program Scope
static int num5; //File Scope
namespace ntest
{ int num4; //Namespace scope
class Test
{ int num3; //Class Scope };
}
void sum( int num1, int num2 ); //Prototype scope
int main( void )
{
int num1 = 10; //Function Scope
while( true )
{ int temp = 0; }
return 0; //Block Scope
}

Sunbeam Infotech www.sunbeaminfo.com


Macro

• Symbolic constant is called as macro


• Expanding macro is a job of pre processor.
• Example:
• #define SIZE 10
• #define EOF -1
• #define MULTIPLY(x,y) x*y
• Few other Macro’s in C++
• __FILE__, __LINE__,__DATE__,__TIME__

Sunbeam Infotech www.sunbeaminfo.com


Association
• If has-a relationship exist between two types then we should use association.
• Example : Car has-a engine (OR engine is part-of car)
• If object is part-of / component of another object then it is called association.
• If we declare object of a class as a data member inside another class then it represents association.
• Example Association:
class Engine
{ };
class Car
{ private:
Engine e; //Association
};
int main( void )
{ Car car;
return 0;
}
//Dependant Object : Car Object
//Dependancy Object : Engine Object

Sunbeam Infotech www.sunbeaminfo.com


Composition and aggregation are specialized form of association
Composition Aggregation
• If dependency object do not exist without • If dependency object exist without Dependant object
Dependant object then it represents composition. then it represents Aggregation.
• Composition represents tight coupling. • Aggregation represents loose coupling.
• Example: Human has-a heart. class Faculty
class Heart { };
class Department
{ };
{
class Human
Faculty f; //Association->Aggregation
{ Heart hrt; //Association->Composition
};
};
int main( void )
int main( void ) {
{ Human h; Department d;
return 0; return 0;
} }
• //Dependant Object : Human Object • //Dependant Object : Department Object
• //Dependancy Object : Heart Object • //Dependancy Object : Faculty Object

Sunbeam Infotech www.sunbeaminfo.com


Inheritance
• If "is-a" relationship exist between two types then we should use inheritance.
• Inheritance is also called as "Generalization".
• Example: Book is-a product
• During inheritance, members of base class inherit into derived class.
• If we create object of derived class then non static data members declared in base class get space inside it.
• Size of object = sum of size of non static data members declared in base class and derived class.
• If we use private/protected/public keyword to control visibility of members of class then it is called access
Specifier.
• If we use private/protected/public keyword to extend the class then it is called mode of inheritance.
• Default mode of inheritance is private.
• Example: class Employee : person //is treated as class Employee : private Person
• Example: class Employee:public Person
• In all types of mode, private members inherit into derived class but we can not access it inside member
function of derived class.
• If we want to access private members inside derived class then:
• Either we should use member function(getter/setter).
• or we should declare derived class as a friend inside base class.

Sunbeam Infotech www.sunbeaminfo.com


Syntax of inheritance in C++

class Person //Parent class In C++ Parent class is called as Base class and child class is
{ }; called as derived class. To create derived class we should use
class Employee : public Person // Child class colon(:) operator. As shown in this code, public is mode of
{ }; inheritance.
class Person //Parent class If we create object of derived class, then all the non- static
{ char name[ 30 ]; int age; }; data member declared in base class & derived class get space
class Employee : public Person //Child class inside it i.e. non-static static data members of base class
{ int empid; float salary; }; inherit into the derived class.
int main( void )
{
Person p;
cout<<sizeof( p )<<endl;
Employee emp;
cout<<sizeof( emp )<<endl;
return 0;
}

Sunbeam Infotech www.sunbeaminfo.com


Syntax of inheritance in C++

• Using derived class name, we can access static data member declared in base class i.e. static data
member of base class inherit into derived class.
class Base{ class Derived : public Base int main( void )
protected: { {
static int number; int num3; Derived d;
static int num4;
}; d.setNum1(10);
public:
int Base::number = 10; d.setNum3(30);
void setNum3( int num3 )
class Derived : public Base{ Derived::setNum2(20);
{ this->num3 = num3; }
public: Derived::setNum4(40);
static void setNum4( int num4 )
static void print( void ) return 0;
{ Derived::num4 = num4; }
{ cout<<Base::number<<endl; } }; }
}; int Derived::num4;
int main( void ){
Derived::print();
return 0;
}
Sunbeam Infotech www.sunbeaminfo.com
Except following functions, including nested class, all the members of base
class, inherit into the derived class
• Constructor
• Destructor
• Copy constructor
• Assignment operator
• Friend function.

Sunbeam Infotech www.sunbeaminfo.com


Types of Inheritance
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
If we combine any two or more types together then it is called as hybrid inheritance.

Sunbeam Infotech www.sunbeaminfo.com


Thank You

Sunbeam Infotech www.sunbeaminfo.com

You might also like