Table of contents
- 1) How to sort vector in C++ ?
- 2) What is Pure Virtual Function in C++ ?
- 3) Difference b/w Virtual Function and Pure Virtual Function.
- 4) What is OOPS in C++?
- 5) What is inheritance in C++?
- 6) What are Objects in C++?
- 7) What is encapsulation in C++?
- 8) What is an abstraction in C++?
- 9) What is a Virtual Base Class in C++?
- Summary
- 10 ) How to call a base class constructor from a derived class in C++?
1) How to sort vector in C++ ?
#include<bits/stdc++.h>
using namespace std;
//custom fuction to sort increasing order
bool sortInc(int a,int b)
{
return a<b;
}
//custom function to sort in decreasing
bool sortDec(int a,int b)
{
return a>b;
}
void print(vector<int>&arr)
{
for(auto it:arr)
{
cout << it << " ";
}
cout << endl;
}
int main()
{
vector<int>vect = {1,45,6,3,69,2};
sort(vect.begin(),vect.end(),sortDec);
print(vect);
}
2) What is Pure Virtual Function in C++ ?
A pure virtual function is a type of virtual function which does not have implementation, but is only declared. It is declared by assigning 0 in declaration.
class Test
{
// Data members of class
public:
virtual void show() = 0;
/* Other members */
};
Pure virtual functions can also be understood as, the functions which needs to be incorporated for a certain class but we don't know how to incorporate it, so the derived class(s) inherit and then know 'How' to incorporate.
For Example, Animal class should have a function move(), but we can't say on a generalize level how animals move. The derived class would answer this let just say (cheetah derives animal, so it can answer that cheetah is an animal which moves by running)
3) Difference b/w Virtual Function and Pure Virtual Function.
Virtual Function | Pure Virtual Function |
A virtual function is a member function in a base class that you expect to override in derived classes. | A pure virtual function is a virtual function that has no implementation in the base class and must be overridden in any derived class. |
Allows derived classes to provide a specific implementation of the function. | Used to create abstract classes, which cannot be instantiated. Forces derived classes to provide their own implementation of the function. |
Can have an implementation in the base class. The derived class may override it, but it is not mandatory. | Does not have an implementation in the base class. Derived classes must override it. |
When you call a virtual function through a base class pointer or reference, the version of the function in the derived class is executed (if it is overridden). | Any class with one or more pure virtual functions is considered abstract. Abstract classes are used as interfaces or base classes in inheritance hierarchies. |
// virtual function
class Base {
public:
virtual void show() {
// base class implementation
}
};
// pure virtual
class Base {
public:
virtual void show() = 0; // pure virtual function
};
#include <iostream>
// Base class with a virtual function
class Base {
public:
virtual void display() {
std::cout << "Display Base class" << std::endl;
}
virtual void show() = 0; // pure virtual function
};
// Derived class implementing both virtual and pure virtual functions
class Derived : public Base {
public:
void display() override {
std::cout << "Display Derived class" << std::endl;
}
void show() override {
std::cout << "Show Derived class" << std::endl;
}
};
int main() {
Base* b; // Pointer to base class
Derived d;
b = &d;
b->display(); // Calls Derived class's display
b->show(); // Calls Derived class's show
return 0;
}
4) What is OOPS in C++?
OOP or Object Oriented Programming in C++ is a type of programming in which you create objects and classes to emulate real-world concepts such as Abstraction, Polymorphism, Encapsulation, and Inheritance.
5) What is inheritance in C++?
Inheritance in C++ is just like a child inherits some features and attributes from his parent similarly a class inherit attributes and methods from another class. The parent class is called base class and the child class is called derived class.
// Base class
class Food_Item{
public:
void taste() {
cout << ""The taste of every food item is different. n"" ;
}
};
// Derived class
class Chips: public Food_Item{
public:
void taste() {
cout << ""The taste of chips is salty n"" ; }
};
6) What are Objects in C++?
Class in C++ provides a blueprint to build objects. That means objects are created from the class.
7) What is encapsulation in C++?
To prevent access to data directly, Encapsulation is the process that combines data variables and functions in a class. This is achieved by doing the following:
Making all data variables private.
Creating getter and setter functions for data variables.
8) What is an abstraction in C++?
Abstraction in C++ means showing only what is necessary. It’s part of the Object-oriented Programming concept. Abstraction is used to hide any irrelevant data from the outside world and only show what is necessary for the outside world to use.
eg. Classes use the abstraction concept to show only relevant data types or elements. This is done through access specifiers such as public, private, and protected.
9) What is a Virtual Base Class in C++?
A virtual base class in C++ helps resolve a specific problem in multiple inheritance, the problem is called the Diamond Problem.
The Diamond Problem means if Class B and Class C inherit Class A and Class D inherits both Class A&B, so without a virtual base classes 'D' would inherit two separate copies of 'A' leading to ambiguity.
// Problem
class A {
public:
void display() {
std::cout << "Class A" << std::endl;
}
};
class B : public A {
};
class C : public A {
};
class D : public B, public C {
};
// Solution
class A {
public:
void display() {
std::cout << "Class A" << std::endl;
}
};
class B : virtual public A {
};
class C : virtual public A {
};
class D : public B, public C {
};
// proper code explanation
#include <iostream>
class A {
public:
int a;
void display() {
std::cout << "Class A" << std::endl;
}
};
class B : virtual public A {
};
class C : virtual public A {
};
class D : public B, public C {
};
int main() {
D obj;
obj.a = 10; // No ambiguity, single instance of A
obj.display(); // Calls A's display
return 0;
}
Summary
Virtual Base Class: A technique to avoid multiple instances of a base class in a multiple inheritance scenario.
Usage: Use the
virtual
keyword in the inheritance declaration.Purpose: Solves the diamond problem and avoids ambiguity by ensuring a single instance of the base class.
10 ) How to call a base class constructor from a derived class in C++?
A base class constructor will be called whenever the derived class constructor is called. Upon the creation of a derived class object, the order of constructor execution is: base class constructor then Derived class constructor.