Mark As Completed Discussion

Object-Oriented Programming

In C++, object-oriented programming (OOP) is a paradigm that allows us to model real-world objects by using classes, objects, inheritance, and polymorphism.

Classes and Objects

A class is a blueprint for creating objects. It defines the properties and behaviors that an object of that class can have. An object is an instance of a class, and it represents a specific entity.

Here's an example of a class and an object in C++:

TEXT/X-C++SRC
1#include <iostream>
2using namespace std;
3
4// Create a class
5
6class Rectangle
7{
8    private:
9        int length;
10        int breadth;
11    public:
12        // Constructor
13        Rectangle()
14        {
15            length = 0;
16            breadth = 0;
17        }
18        // Parameterized Constructor
19        Rectangle(int l, int b)
20        {
21            length = l;
22            breadth = b;
23        }
24        // Member function
25        int area()
26        {
27            return length * breadth;
28        }
29};
30
31int main()
32{
33    // Create object of class Rectangle
34    Rectangle r1;
35    // Access member function using object
36    cout << "Area: " << r1.area() << endl;
37
38    // Create object of class Rectangle with parameterized constructor
39    Rectangle r2(10, 20);
40    // Access member function using object
41    cout << "Area: " << r2.area() << endl;
42
43    return 0;
44}

In this example, we define a class called Rectangle which has two private variables length and breadth. We have a default constructor and a parameterized constructor to initialize the class variables. The class also has a member function area() that calculates and returns the area of the rectangle.

We create objects r1 and r2 of the Rectangle class. We can then access the member function area() using these objects and print the area.

Inheritance

Inheritance is a mechanism that allows a class to inherit the properties and behaviors of another class. The class that is inherited from is called the base class or parent class, and the class that inherits is called the derived class or child class.

Here's an example of inheritance in C++:

TEXT/X-C++SRC
1#include <iostream>
2using namespace std;
3
4// Base class
5
6class Shape
7{
8    protected:
9        int width;
10        int height;
11    public:
12        // Constructor
13        Shape(int w, int h)
14        {
15            width = w;
16            height = h;
17        }
18        // Print function
19        void print()
20        {
21            cout << "Width: " << width << endl;
22            cout << "Height: " << height << endl;
23        }
24};
25
26// Derived class
27
28class Rectangle: public Shape
29{
30    public:
31        // Constructor
32        Rectangle(int w, int h): Shape(w, h) {}
33        // Area function
34        int area()
35        {
36            return width * height;
37        }
38};
39
40int main()
41{
42    // Create object of class Rectangle
43    Rectangle r(5, 3);
44    // Access member function of base class
45    r.print();
46    // Access member function of derived class
47    cout << "Area: " << r.area() << endl;
48
49    return 0;
50}

In this example, we define a base class called Shape with two protected variables width and height, and a constructor to initialize these variables. The base class also has a print() function to print the width and height.

We then define a derived class called Rectangle that inherits from the Shape class using the public access specifier. The derived class has its own constructor that calls the base class constructor. It also has a member function area() to calculate and return the area of the rectangle.

We create an object r of the Rectangle class. We can access the member function print() from the base class using the object r, and we can access the member function area() from the derived class using the same object. We can print the area using cout.

Polymorphism

Polymorphism is a feature of OOP that allows objects of different classes to be treated as objects of a common parent class. It allows for code reusability and flexibility.

Here's an example of polymorphism in C++:

TEXT/X-C++SRC
1#include <iostream>
2using namespace std;
3
4// Base class
5
6class Shape
7{
8    protected:
9        int width;
10        int height;
11    public:
12        // Constructor
13        Shape(int w, int h)
14        {
15            width = w;
16            height = h;
17        }
18        // Area function (to be overridden)
19        virtual int area() = 0;
20};
21
22// Derived class
23
24class Rectangle: public Shape
25{
26    public:
27        // Constructor
28        Rectangle(int w, int h): Shape(w, h) {}
29        // Area function (override)
30        int area()
31        {
32            return width * height;
33        }
34};
35
36// Derived class
37
38class Triangle: public Shape
39{
40    public:
41        // Constructor
42        Triangle(int w, int h): Shape(w, h) {}
43        // Area function (override)
44        int area()
45        {
46            return (width * height) / 2;
47        }
48};
49
50int main()
51{
52    // Create array of objects of base class
53    Shape* shapes[2];
54    // Create objects of derived classes
55    Rectangle r(5, 3);
56    Triangle t(4, 6);
57    // Assign objects to array
58    shapes[0] = &r;
59    shapes[1] = &t;
60
61    // Access member function using objects in array
62    for (int i = 0; i < 2; i++)
63    {
64        cout << "Area: " << shapes[i]->area() << endl;
65    }
66
67    return 0;
68}

In this example, we define a base class called Shape with two protected variables width and height, and a constructor to initialize these variables. The base class also has a pure virtual member function area() using = 0, indicating that the function is to be overridden by the derived classes.

We then define two derived classes called Rectangle and Triangle that inherit from the Shape class using the public access specifier. These derived classes have their own constructors that call the base class constructor. They also have their own implementations of the area() function to calculate and return the area of each shape.

We create an array of objects shapes of the base class Shape. We also create objects r of the Rectangle class and t of the Triangle class. We assign these objects to the shapes array.

We then use a loop to access the member function area() using the objects in the shapes array. We can print the area using cout.

Conclusion

Object-oriented programming in C++ allows us to model real-world objects and their relationships using classes, objects, inheritance, and polymorphism. Understanding these concepts is essential for building complex applications with reusable and maintainable code. By incorporating OOP principles, we can design and implement more efficient and organized C++ programs that align with industry standards and best practices.

CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment