C++ Unit 4
C++ Unit 4
This means C++ has the ability to provide the operators with a
special meaning for a data type, this ability is known as operator
overloading.
int main()
{
Test tt;
++tt; // calling of a function "void operator
++()"
tt.Print();
return 0;
}
Operator Overloading in Unary Operators
In unary operator function, no arguments should be
passed.
It works only with one class objects. It is a overloading
of an operator operating on a single operand.
The Unary minus ( – ) operator, Logical not ( ! )
operator,
Increment operator ++ and decrement operator -- are
examples of unary operators.
When unary operators are overloaded through a
member function take no explicit arguments, but, if
they are overloaded by a friend function, takes one
argument.
Syntax:
return_type :: operator binary_operator_symbol ()
{
// function definition
}
Example:
#include<iostream.h>
#include<conio.h>
class unary
{
int a;
float b;
public:
void getdata()
{
cout<<"\n enter an integer value:";
cin>>a;
cout<<"\nenter the float value:";
cin>>b;
}
void putdata()
{
cout<<"a="<<a<<"\t"<<"b="<<b<<endl;
}
void operator++()
{
++a;
++b;
}
void operator--()
{
--a;
--b;
}
void operator-()
{
a=-a;
b=-b;
}
};
int main()
{
clrscr();
cout<<"\n\t\tUNARY OPERATOR OVERLOADING";
unary u;
u.getdata();
cout<<"\npre-decreament operator overloading\n";
--u;
u.putdata();
cout<<"unary minus operator overloading\n";
-u;
u.putdata();
cout<<"pre-increament operator overloading\n";
++u;
u.putdata();
getch();
return 0;
}
Output
Overloading Binary Operator
Binary operators work on two operands.
Example:
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
void getdata()
{
cout<<"\n enter the 2 float values:";
cin>>x>>y;
}
void putdata()
{
if(y>0)
cout<<x<<"+j"<<y<<endl;
else
{
y=-y;
cout<<x<<"-j"<<y<<endl;
}
}
complex operator+(complex);
friend complex operator-(complex,complex);
};
complex complex::operator+(complex c)
{
complex t;
t.x=x+c.x;
t.y=y+c.y;
return t;
}
complex operator-(complex t1,complex t2)
{
complex t3;
t3.x=t1.x-t2.x;
t3.y=t1.y-t2.y;
return t3;
}
int main()
{
clrscr();
cout<<"\t\tBINARY OPERATOR OVERLOADING";
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1+c2;
cout<<"addition of 2 complex numbers\n";
c3.putdata();
c3=c1-c2;
cout<<"subtraction of 2 complex numbers\n";
c3.putdata();
getch();
return 0;
}
OUTPUT:
Inheritance
In C++, inheritance is a process in which one object acquires
all the properties and behaviors of its parent object
automatically.
Derived Classes
A Derived class is defined as the class derived from the base
class.
The Syntax of Derived class:
class derived_class_name : visibility-mode base_class_name
{
// body of the derived class.
}
Where,
derived_class_name: It is the name of the derived class.
visibility mode: The visibility mode specifies whether the
features of the base class are publicly inherited or privately
inherited.
It can be public or private.
Base_class_name: It is the name of the base class.
Modes of Inheritance
Public mode: If we derive a sub class from a public base
class. Then the public member of the base class will
become public in the derived class and protected members
of the base class will become protected in derived class.
Example:
#include <iostream>
// base class
class Animal
{
public:
void eat()
{
cout << "I can eat!" << endl;
}
void sleep()
{
cout << "I can sleep!" << endl;
}
};
// derived class
class Dog : public Animal
{
public:
void bark()
{
cout << "I can bark" << endl;
}
};
int main() {
// Create object of the Dog class
Dog dog1;
return 0;
}
Output:
I can eat!
I can sleep!
I can bark
Multilevel Inheritance
In this type of inheritance, a derived class is created
from another derived class.
Inheritance is transitive so the last derived class
acquires all the members of all its base classes.
Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Output:
Eating…
Barking…
Weeping…
Multiple Inheritance
Multiple inheritance is the process of deriving a new
class that inherits the attributes from two or more
classes.
i.e one sub class is inherited from more than one base
classes.
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2,
....
{
// body of subclass
};
Example:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10); Output:
c.get_b(20); The value of a is : 10
c.display(); The value of b is : 20
return 0; Addition of a and b is : 30
}
Hierarchical Inheritance
Hierarchical inheritance is defined as the process of
deriving more than one class from only one base class.
Example:
#include<iostream>
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
// first sub class
class Car: public Vehicle
{
};
};
Output
// main function
int main() This is a Vehicle
{ This is a Vehicle
Car obj1;
Bus obj2;
return 0;
}
Hybrid Inheritance
Hybrid (Virtual) Inheritance: Hybrid Inheritance
is implemented by combining more than one type of
inheritance.
For example: Combining Hierarchical inheritance
and Multiple Inheritance.
Below image shows the combination of hierarchical
and multiple inheritance:
Example: ( Practical Program)
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollnum;
public:
void getnum()
{
cout<<"\nenter the roll number:";
cin>>rollnum;
}
void putnum()
{
cout<<"\n roll no:"<<rollnum;
}
};
class test:public student
{
protected:
float mark1,mark2;
public:
void getmarks()
{
cout<<"\nenter mark1(<=50):";
cin>>mark1;
cout<<"\nenter mark2(<=50):";
cin>>mark2;
}
void putmarks()
{
cout<<"\n mark1:"<<mark1;
cout<<"\n mark2:"<<mark2;
}
};
class sports
{
protected:
float score;
public:
if(total<50)
cout<<"\nthe student is fail";
else void getscore()
{
cout<<"\nenter the score(0-10):";
cin>>score;
}
void putscore()
{
cout<<"\nscore:"<<score;
}
};
class result:public test,public sports
{
float total;
public:
void display();
};
void result::display()
{
getnum();
getmarks();
getscore();
total=mark1+mark2+score;
putnum();
putmarks();
putscore();
cout<<"\ntotal:"<<total;
class A
{
public:
void show()
{
cout << "Hello form A \n";
}
};
class B : public A
{
};
class C : public A
{
};
class D : public B, public C
{
};
int main()
{
D object;
object.show();
}
How to resolve this issue?
To resolve this ambiguity when class A is inherited in
both class B and class C, it is declared as virtual base
class by placing a keyword virtual as :
Syntax for Virtual Base Classes
Syntax 1:
class B : virtual public A
{
};
Syntax 2:
class C : public virtual A
{
};
Note:
class A
{
public:
int a;
A() // constructor
{
a = 10;
}
};
int main()
{
D obj;
cout << "a = " << obj.a << endl;
return 0;
}
Output:
a = 10