Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

C++ Internal Two - Answers

Abstract Class.

A class containing only declarations of functions but not definitions. We use the define functions in
abstract class as we need in the derived classes ( child classes ). This is often used when we know
that functions will be needed, but we don't know what exactly the functions does.

Even though we say that a class can be made abstract, C++ doesn't have a keyword ‘abstract’. We
make a class abstract by defining pure virtual functions in the class.

Pure Virtual Functions.

These Functions don't have a definition (body) part. Instead, they are initialized to zero. A function
can be made virtual using the keyword ‘virtual‘.

E.g:- virtual void show() = 0

We use these inside abstract classes mostly. Even if a class contains only one virtual function, the
class is said to be abstract. If we didn’t override the virtual function in derived ( child ) class, the
derived class also becomes abstract.

Example Program:-

#include<iostream>
using namespace std;

class BaseClass // Base Class Which is Abstract


{
public:
int var;
virtual void show() = 0;
BaseClass(int val) // Abstract Classes Can Have Constructors
{
x = val;
}
};

class DerivedClass: public BaseClass


{
public:
int vard;
DerivedClass(int val1, int val2):Base(val1) // DerivedClass --> BaseClass
{
y = j;
}
void show()
{
cout << "BaseClass Value = " << var << ", DerivedClass Value = " << vard<<endl;
}
};

int main(void)
{
DerivedClass d(4, 2);
d.show();
return 0;
}

The Output Will be :-

BaseClass Value = 4 ,DerivedClass Value = 2

Object Slicing in C++

In C++ we can assign a derived class object to base class. But, in this case, the derived class
variables and functions will not be accessible from the assigned base class object. This is known as
object slicing in C++. The sliced attributes will be the attribute of the derived class.

Example Program:-

#include<iostrem>
using namespace std

class BaseClass{
public:
int bvar;
BaseClass(int num)
{
bvar = num;
}
void show()
{
cout<<”Base Class = “<<bvar<<endl;
}
};

class DerivedClass : public BaseClass


{
public:
int dvar;
DerivedClass(int num1,int num2) : BaseClass(num1)
{
dvar = num2;
}
void show()
{
cout<<”Derived Class = “<<dvar<<” and “<<bvar<<endl;
}
};

int main()
{
BaseClass a(5);
DerivedClass b(10,15);
a.show()
b.show()
BaseClass c = b; // Object Slicing Will Happen Here
c.show()
}

The Output Will Be :-

BaseClass = 5
DerivedClass = 10 and 15
BaseClass = 10 // Output From Sliced Object

Even though we can use object slicing as a favor, mistakenly it can be an unexpected error in our
program. So we make use of pointers and references in C++ to avoid object slicing.

Eg:-
Redefining the above main function.

int main()
{
BaseClass a(5);
DerivedClass b(10,15);
a.show()
b.show()
BaseClass &c = b; // Object Slicing Will Not Happen Here
c.show()
}
The Output Will Be :-

BaseClass = 5
DerivedClass = 10 and 15
DerivedClass = 10 and 15 // Output From Assigned object
Diamond Problem in C++

The diamond problem happens due to multiple inheritance in C++. Multiple inheritance means a
class is derived from two or more base classes. Technically, it is possible to do so. But, what if the
base classes are derived classes of another class ( ie, the base classes are child classes of another
one. So these classes can be called child/parent class for now).

So now, our child class is derived from child/parent classes. Child/parent classes are derived from
the same parent class through hierarchical inheritance. So all these child/parent classes will have
one instance of the parent class. So through the child/parent classes, our child class receives an
instance of the parent class multiple times. This is known as Diamond Problem.

We can solve this problem by making the inheritance from parent class to child/parent classes as
virtual using the keyword ‘virtual’.

Example Program:-

#include<iostream>
using namespace std;

class Parent
{
Parent(){ cout<<”Parent”<<endl; }
};

class ChildParentOne : public Parent // change to ChildParent : virtual public Parent


{
ChildParentOne(){ cout<<”ChildParentOne”<<endl; }
};

class ChildParentTwo : public Parent // change to class ChildParentTwo : virtual public Parent
{
ChildParentTwo(){ cout<<”ChildParentTwo”<<endl; }
};

class Child : public ChildParentOne, public ChildParentTwo


{
Child(){ cout<<”Child”<<endl;
};

int main()
{
Child c;
}

The output will be :-

Parent
ChildParentOne
Parent
ChildParentTwo
Child
In the output, we can see that the Parent is printed 2 times. We can solve this by changing those
lines as shown in the comment. Then the output will be

Parent
ChildParentOne
ChildParentTwo
Child

Thats it !

Operator Overloading in C++

Operator overloading is redefining an operator to do some other tasks. By default, C++ has some
inbuilt operator overloading mechanisms. Apart from that users can also define an operator. This is
known as operator overloading.

Why we need operator overloading?

Mostly we need operator overloading when we work with some user-defined classes. The most
known rookie example is complex number addition.

In operator overloading, we can overload almost every operator in C++, except ‘.’ dot operator, ‘::’
Scope Resolution, ‘?:’ Conditional, sizeof Operators.

Examples:-

Inbuilt Operator Overloading

#include<iostream>
using namespace std;

int main()
{
int a = 1 + 5; // + acts as an addition operator
cout<<a<<endl;

String s = “Hello” + “ World!” // + Acts an concatination operator


cout<<s<<endl;
}

The Output Will be :-


6
Hello World!

User Defined Operator Overloading

#include<iostream>
using namespace std;
class Complex
{
int real,img;
public:
Complex(int a,int b)
{
real = a;
img = b;
}
void display()
{
cout<<"real = "<<real<<" imaginary = "<<img<<endl;
}
friend Complex operator + (Complex c1, Complex c2) ; // friend function
};

Complex operator + ( Complex c1, Complex c2)


{
return Complex(c1.real + c2.real, c1.img + c2.img);
}

int main()
{
Complex c1(2,3) , c2(3,5);
Complex c3 = c1 + c2; // Operator Overloaded
c3.display();
return 0;
}

The Output Will be :-

real = 5 imaginary = 8

You might also like