0% found this document useful (0 votes)
2K views

Abstract Base Classes and Concrete Classes

The document discusses abstract base classes and concrete classes in C++. It defines abstract classes as classes that cannot be instantiated and must be inherited from, while concrete classes can be instantiated. Abstract classes may contain abstract methods that are declared but not defined, while concrete classes have fully defined methods. The document provides examples of defining abstract and concrete classes, and using pure virtual functions to define abstract classes and interfaces. It also covers the differences between abstract and concrete classes, and early binding versus late binding of function calls.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Abstract Base Classes and Concrete Classes

The document discusses abstract base classes and concrete classes in C++. It defines abstract classes as classes that cannot be instantiated and must be inherited from, while concrete classes can be instantiated. Abstract classes may contain abstract methods that are declared but not defined, while concrete classes have fully defined methods. The document provides examples of defining abstract and concrete classes, and using pure virtual functions to define abstract classes and interfaces. It also covers the differences between abstract and concrete classes, and early binding versus late binding of function calls.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Abstract Base Classes and

Concrete Classes
Abstract classes
All classes can be designated as either abstract or
concrete.
Concrete is the default.
This means that the class can have (direct) instances.
In contrast, abstract means that a class cannot have its
own (direct) instances.
A class that contains a pure virtual function is known
as an abstract class.
Abstract class
Abstract classes may or may not contain abstract
methods, i.e., methods without body ( public void
get(); )
You cannot instantiate an abstract class.
An abstract class may contain abstract methods.
You need to inherit an abstract class to use it.
If you inherit an abstract class, you have to provide
implementations to all the abstract methods in it.
// Abstract class
class Shape
{
protected:
float dimension;
public:
void getDimension()
{
cin >> dimension;
}
// pure virtual Function
virtual float calculateArea() = 0; };
// Derived class
class Square : public Shape
{
public:
float calculateArea()
{
return dimension * dimension;
}
};
// Derived class
class Circle : public Shape
{
public:
float calculateArea()
{
return 3.14 * dimension * dimension; }
};
int main()
{
Square square;
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() <<
endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() <<
endl; return 0;
}
pure virtual function
A pure virtual function doesn't have the function body and it
must end with = 0.
For example,
class Shape
{ public:
// creating a pure virtual function virtual void calculateArea()
= 0;
};
Note: 
The = 0 syntax doesn't mean we are assigning 0 to the
function. It's just the way we define pure virtual functions.
Concreate class

A concrete class is an ordinary class which has no


purely virtual functions and hence can be
instantiated. 
A concrete class doesn’t have any abstract methods.
It is not mandatory to inherit a concrete class to use it.
Example
class ConcreteClassExample
{
public int add(int a, int b)
{
int c = a + b;
return c;
}
public int subtract(int a, int b)
{
int c = a - b; return c;
}
public void display(){
Cout<<“Hi welcome";
}
int main()
{
ConcreteClassExample obj = new
ConcreteClassExample();
cout<<obj.add(25, 347);
cout<<obj.subtract(500, 456);
obj.display();
}
}
Output
372
44
Hi welcome
Pure Abstract Classes
An abstract class is one in which there is a declaration
but no definition for a member function.
The way this concept is expressed in C++ is to have the
member function declaration assigned to zero.
Example
class PureAbstractClass
{
public: virtual void AbstractMemberFunction() = 0;
};
Pure Abstract Classes
A pure Abstract class has only abstract member
functions and no data or concrete member functions.
 In general, a pure abstract class is used to define an
interface and is intended to be inherited by concrete
classes.
#include<iostream>
using namespace std;
class Sample_Class {
int a = 5;
public:
virtual void sample_func() = 0;
void print_func() {
cout << a;
}
};
class Derived_Class : public Sample_Class {
public:
void sample_func() {
cout << "pure virtual function is implemented";
}
};
int main() {
Derived_Class d_object;
d_object.sample_func();
}
OUTPUT: pure virtual function is implemented
Concrete Class
Abstract Class
Differences between Abstract Class and Concrete Class

Type Base class Default class


All methods are
Methods May contain partially completely
implemented methods implemented
Some or all declared
Functions functions are purely No purely virtual
virtual functions

Instantiation Cannot be instantiated Can be instantiated


Binding
Binding refers to the process of converting
identifiers (such as variable and performance
names) into addresses.
Binding is done for each variable and functions. ‘
For functions, it means that matching the call with the
right function definition by the compiler. It takes place
either at compile time or at runtime
Early Binding (compile-time time
polymorphism)
 As the name indicates, compiler (or linker) directly
associate an address to the function call.
It replaces the call to the address of the function.
By default early binding happens in C++.
Late binding (discussed below) is achieved with the
help of  virtual keyword.
#include<iostream>     void show() { cout<<"In Derived
using namespace std; \n"; }
     };
class Base     
{ int main(void)
public: {
    void show() { cout<<" In Base     Base *bp = new Derived;
\n"; }     // The function call decided at 
}; compile time (compiler sees type of
     pointer and calls base class
function.
class Derived: public Base
    bp->show();  
{
  
public:
    return 0;
}
OUTPUT:
Enter the length of the square: 4
Area of square: 16
Enter radius of the circle: 5
Area of circle: 78.5
#include<iostream>
using namespace std;

class Base
{
public:
void show() { cout<<" In Base \n"; }
};

class Derived: public Base


{
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived;

// The function call decided at compile time (compiler


sees type of pointer and calls base class function.
bp->show();

return 0;
}

You might also like