• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Interfaces in C++ (Abstract Classes in C++)

November 5, 2014 by Dhaval Dave

Capabilities of a C++ class without committing to a particular implementation of that class.
The C++ interfaces are implemented using abstract classes

A class is made abstract by declaring at least one of its functions as pure virtual function. A pure virtual function is specified by placing “= 0” in its declaration as follows:

class Shape 
{
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.

Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.

Classes that can be used to instantiate objects are called concrete classes.

// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};

int main(void)
{
Rectangle Rect;
Triangle Tri;

Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout
<< "Total Rectangle area: " << Rect.getArea() << endl;

Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout
<< "Total Triangle area: " << Tri.getArea() << endl;

return 0;
}

When the above code is compiled and executed, it produces the following result:

Total Rectangle area: 35
Total Triangle area: 17

Similar Articles

Filed Under: problem Tagged With: c

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Difference between a LinkedList and a Binary Search Tree BST

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

HackeEarth Flipkart’s Drone

Generate largest number arranging a no. of given non negative integer numbers

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Fibonacci Hashing & Fastest Hashtable

K’th Largest Element in BST when modification to BST is not allowed

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Leetcode: Merge Intervals

Reverse a Linked List in groups of given size

Find Percentage of Words matching in Two Strings

Connect n ropes with minimum cost

Reversal of LinkedList

Implement a generic binary search algorithm for Integer Double String etc

ADOBE Aptitude C Language Test

Check if an array has duplicate numbers in O(n) time and O(1) space

The greedy coins game Dynamic Programming

Find loop in Singly linked list

Wrong Directions given find minimum moves so that he can reach to the destination

LeetCode: Container With Most Water

Coin Collection Dynamic Programming

Find the smallest window in a string containing all characters of another string

Facebook Interview Question : Interleave List

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Print Power Set of a Set

Check Binary Tree is Binary Search Tree or not

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Find min element in Sorted Rotated Array (Without Duplicates)

Copyright © 2025 · Genesis Framework · WordPress · Log in