0% found this document useful (0 votes)
10 views8 pages

Types of C++ Classes Explained

CPP class types notes

Uploaded by

satyam pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Types of C++ Classes Explained

CPP class types notes

Uploaded by

satyam pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Detailed Notes: Different Types of Classes in C++

Classes in C++ are fundamental building blocks of Object-Oriented Programming (OOP). They allow you to
create user-defined data types that encapsulate data (attributes) and behavior (methods). Classes can be
categorized based on their design, purpose, and role in an inheritance hierarchy.

1. Concrete Class

Definition: A concrete class is a class that can be instantiated, meaning you can create objects from it. It
contains complete definitions of functions and data members.

Uses:

• Most commonly used class in C++ programs.


• Used to define actual objects with behavior and data.

Example in Real World:

• A Car class with attributes like model and color and methods like drive() or brake() .

Programming Use Case:

class Car {
public:
void drive() { cout << "Driving..."; }
};
Car myCar;
[Link]();

Why called 'Concrete':

• "Concrete" implies completeness. Unlike abstract classes, it’s not a blueprint—it can be directly used
to create usable objects.

Interview Questions and Answers:

• What is a concrete class?


• A class with complete implementation that can be instantiated.
• Can you give an example of a concrete class?
• class Car { public: void drive(); };
• How is a concrete class different from an abstract class?
• Concrete class has complete definitions; abstract has at least one pure virtual function.
• Why do we use concrete classes in applications?
• To create usable objects with defined behaviors.

1
• What are the default access specifiers in a class?
• Private.
• How would you design a concrete class for a real-world entity like Car or Employee?
• Include attributes and behaviors relevant to the entity with full method definitions.

2. Abstract Class

Definition: A class that contains at least one pure virtual function. It cannot be instantiated.

Uses:

• Provides an interface for derived classes.


• Helps in achieving runtime polymorphism.

Example in Real World:

• An Animal class with a pure virtual function makeSound() that must be defined by derived
classes like Dog or Cat .

Programming Use Case:

class Animal {
public:
virtual void makeSound() = 0; // Pure virtual
};

class Dog : public Animal {


public:
void makeSound() override { cout << "Bark"; }
};

Why called 'Abstract':

• "Abstract" denotes incompleteness; it's a concept or blueprint without full implementation.

Interview Questions and Answers:

• What is an abstract class?


• A class with at least one pure virtual function.
• Can we create an object of an abstract class?
• No.
• What is a pure virtual function? How do you declare it?
• A function with = 0 in its declaration: virtual void foo() = 0;
• How is an abstract class different from an interface?

2
• Abstract class can have data members and some implementation; interface has only pure virtual
functions.
• Why and when do you use an abstract class?
• To define a common interface and enforce method implementation in derived classes.
• Give a real-life example of where an abstract class would be useful.
• Base class Shape with pure virtual draw() used by Circle , Rectangle , etc.
• What happens if a class inherits from an abstract class but doesn’t implement all pure virtual
functions?
• It also becomes abstract and cannot be instantiated.

3. Interface Class (Pure Abstract Class)

Definition: A class in which all methods are pure virtual. It contains no data members and cannot be
instantiated.

Uses:

• Defines a strict contract for subclasses.


• Useful for creating plug-in architectures or enforcing APIs.

Example in Real World:

• Shape interface requiring draw() and area() methods.

Programming Use Case:

class Shape {
public:
virtual void draw() = 0;
virtual float area() = 0;
};

Why called 'Interface':

• Because it defines only the interface (not implementation), similar to Java interfaces.

Interview Questions and Answers:

• How do you implement interface-like behavior in C++?


• By creating a class with only pure virtual functions.
• Can a class have only pure virtual functions? What is that called?
• Yes, it's called a pure abstract class or interface class.
• What’s the difference between an interface and an abstract class?
• Interface has only pure virtual functions and no data; abstract class may have both.

3
4. Abstract vs Interface

• Abstract classes may have some implemented methods.


• Interfaces (pure abstract classes) have only pure virtual methods.

5. Local Class

Definition: A class defined within a function body.

Uses:

• Used when a class is needed only in a specific function's scope.


• Useful for encapsulating helper logic.

Example in Real World:

• A temporary helper class used for sorting or filtering data within a function.

Programming Use Case:

void fun() {
class Helper {
public:
void sayHi() { cout << "Hi"; }
} h;
[Link]();
}

Why called 'Local':

• Its scope is local to the function it's defined in.

6. Nested Class

Definition: A class defined inside another class.

Uses:

• To logically group classes that are only used in one place.


• Helps in encapsulating implementation details.

Example in Real World:

• A Laptop class with a nested Battery class.

4
Programming Use Case:

class Laptop {
public:
class Battery {
public:
void charge() { cout << "Charging..."; }
};
};
Laptop::Battery b;
[Link]();

Why called 'Nested':

• Because it's nested inside another class.

Interview Questions and Answers:

• What is a nested class?


• A class declared inside another class.
• Why would you use a nested class in C++?
• For logical grouping and encapsulation.
• Can a nested class access private members of the outer class?
• No, unless it is declared as a friend.

7. Container Class

Definition: A class designed to hold and manage multiple objects.

Uses:

• Provides storage and access mechanisms for groups of objects.


• STL containers (like vector , map ) are examples.

Example in Real World:

• A Warehouse class storing multiple Product objects.

Programming Use Case:

#include <vector>
class Container {
vector<int> data;
public:

5
void add(int x) { data.push_back(x); }
};

Why called 'Container':

• It contains and manages other objects.

Interview Questions and Answers:

• What is a container class?


• A class that stores and organizes multiple objects.
• Give examples of container classes in STL.
• vector , list , map , set , queue .
• What are the differences between vector, list, and map?
• vector is dynamic array, list is doubly linked list, map is key-value store.
• Why is STL important in C++?
• Provides reusable, efficient, and standardized data structures.

8. Wrapper Class

Definition: A class that wraps a primitive type or an object to add extra functionality.

Uses:

• Used in STL, GUI libraries, and legacy code wrappers.


• Helps in adding features like logging, validation.

Example in Real World:

• A class wrapping an int to add overflow detection.

Programming Use Case:

class IntWrapper {
int value;
public:
IntWrapper(int val) : value(val) {}
int get() const { return value; }
};

Why called 'Wrapper':

• Because it wraps another type or object.

6
9. Derived Class

Definition: A class that inherits from another class (base class).

Uses:

• Promotes reusability and polymorphism.

Example in Real World:

• A Manager class derived from Employee class.

Programming Use Case:

class Employee {
public:
void work() { cout << "Working"; }
};
class Manager : public Employee {
public:
void manage() { cout << "Managing"; }
};

Why called 'Derived':

• Because it is derived from an existing class (inherits from it).

Interview Questions and Answers:

• What is a derived class?


• A class that inherits from another class.
• How do you implement inheritance in C++?
• Using : symbol and access specifier: class B : public A .
• Can a derived class override a base class function? How?
• Yes, using the same function signature and override keyword.
• What is method overriding vs method overloading?
• Overriding: same signature in derived class. Overloading: same function name, different
parameters.

Final Note (Your Question):

Q: Is abstract class used to create another class, unlike other classes which are used to create
objects?

7
Answer: Yes, that’s correct. The main purpose of an abstract class is to act as a base or parent class for
other classes. It defines a common interface or blueprint but does not provide full implementation, so it
cannot be instantiated.

Other classes (like concrete, wrapper, derived) are used to create actual objects and contain complete
implementations.

This is why abstract classes are sometimes referred to as "blueprint classes".

Let me know if you’d like to add UML diagrams, flowcharts, or practice questions with answers.

You might also like