WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Intermediate · question 21 of 100

What are the key features of object-oriented programming (OOP) in C++?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

Object-oriented programming (OOP) is a programming paradigm that is based on the concept of objects, which are instances of classes that encapsulate data and behavior. C++ is an object-oriented programming language that supports a range of OOP features, including:

Encapsulation: This is the principle of hiding the implementation details of a class from other parts of the program, and providing a well-defined interface for accessing and manipulating the data and behavior of the class. Encapsulation is achieved in C++ through the use of access specifiers like public, private, and protected.

Inheritance: This is the principle of creating a new class (the derived class) from an existing class (the base class), which inherits the data and behavior of the base class. In C++, inheritance is supported through the use of the ’class’ keyword and the access specifiers.

Polymorphism: This is the principle of allowing a single function or method to operate on objects of different classes in different ways. Polymorphism is achieved in C++ through the use of virtual functions, function overloading, and templates.

Abstraction: This is the principle of modeling complex systems by defining simplified interfaces that hide the underlying complexity. Abstraction is achieved in C++ through the use of abstract classes and interfaces.

Here is an example of a simple C++ class that demonstrates some of these OOP features:

    #include <iostream>
    
    class Shape {
        public:
        virtual double getArea() = 0;
    };
    
    class Rectangle : public Shape {
        private:
        double width;
        double height;
        public:
        Rectangle(double w, double h) : width(w), height(h) {}
        double getArea() { return width * height; }
    };
    
    class Circle : public Shape {
        private:
        double radius;
        public:
        Circle(double r) : radius(r) {}
        double getArea() { return 3.14 * radius * radius; }
    };
    
    int main() {
        Shape* s1 = new Rectangle(5, 10);
        Shape* s2 = new Circle(7);
        std::cout << "Rectangle area: " << s1->getArea() << std::endl;
        std::cout << "Circle area: " << s2->getArea() << std::endl;
        delete s1;
        delete s2;
        return 0;
    }

In this example, we define two classes, Rectangle and Circle, which both inherit from a Shape base class. The Shape class defines a virtual function called getArea(), which is overridden by the Rectangle and Circle classes. The main() function creates instances of the Rectangle and Circle classes using the ’new’ keyword and stores them in Shape pointers. The getArea() function is then called on each of the Shape pointers to calculate the area of the shapes, and the results are printed to the console.

In summary, C++ supports a range of OOP features including encapsulation, inheritance, polymorphism, and abstraction. These features allow programmers to model complex systems by defining objects and classes that encapsulate data and behavior, and provide a simplified interface for accessing and manipulating that data and behavior.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics