C++

Object Oriented Programming in C++

1027
0
Object Oriented Programming in C++

Object-oriented programming (OOP) is a programming paradigm that is based on the concept of objects, which can contain data and code to manipulate that data. C++ is a powerful object-oriented programming language that supports many of the features of OOP.

Classes and Objects

In C++, objects are instances of classes. A class is a blueprint or template for creating objects. It defines the data and behavior of the objects that will be created from it. Here is an example of a simple class definition:

class Person {
public:
  std::string name;
  int age;
};
C++

This class defines a Person object with two member variables: name of type std::string and age of type int. The public keyword specifies that these member variables are accessible from outside the class.

To create an instance of this class, we can declare a variable of type Person:

Person p;
C++

This creates a Person object named p. We can then set the member variables:

p.name = "Alice";
p.age = 25;
C++

We can also define member functions in a class to manipulate the object’s data. Here is an example:

class Person {
public:
  std::string name;
  int age;

  void printInfo() {
    std::cout << "Name: " << name << ", Age: " << age << std::endl;
  }
};
C++

This Person class now has a printInfo() member function that prints the object’s name and age member variables.

We can call this member function on a Person object:

Person p;
p.name = "Alice";
p.age = 25;
p.printInfo();
C++

This will output: Name: Alice, Age: 25.

Encapsulation

Encapsulation is a fundamental concept in object-oriented programming that refers to the practice of keeping the internal details of an object hidden from the outside world. In C++, this is achieved by declaring the class’s data members as private:

class Person {
private:
  std::string name;
  int age;
public:
  void setName(std::string n) {
    name = n;
  }
  void setAge(int a) {
    age = a;
  }
  void printInfo() {
    std::cout << "Name: " << name << ", Age: " << age << std::endl;
  }
};
C++

In this example, we have declared the name and age member variables as private. This means that they can only be accessed from within the class, and not from outside the class.

We have also defined two public member functions, setName() and setAge(), that can be used to set the name and age member variables. These member functions provide a controlled interface for modifying the internal state of the Person object.

Inheritance

Inheritance is another important concept in object-oriented programming that allows a class to inherit properties and methods from another class. In C++, we can use the class keyword to define a new class that inherits from an existing class:

class Student : public Person {
public:
  int studentId;
};
C++

In this example, we have defined a Student class that inherits from the Person class. The public keyword specifies that the public members of the Person class will be accessible from the Student class.

The Student class also has a new member variable, studentId. We can create a Student object and set its member variables just like we did with the Person object:

Student s;
s.setName("Bob");
s.setAge(20);
s.studentId = 12345;
s.printInfo();
C++

This will output: Name: Bob, Age: 20.

The Student object has access to the printInfo() member function that was defined in the Person class, because it inherited this member function. However, the Student object cannot access the name and age member variables directly, because they were declared as private in the Person class.

Polymorphism

Polymorphism is the ability of objects to take on different forms or behaviors depending on the context in which they are used. In C++, we can achieve polymorphism through virtual functions.

A virtual function is a member function that is declared as virtual in a base class and overridden in a derived class. When a virtual function is called on a derived class object through a base class pointer or reference, the correct function is called based on the actual type of the object.

Here is an example of virtual functions:

class Shape {
public:
  virtual double area() = 0;
};

class Circle : public Shape {
private:
  double radius;
public:
  Circle(double r) : radius(r) {}
  double area() override {
    return 3.14 * radius * radius;
  }
};

class Rectangle : public Shape {
private:
  double width;
  double height;
public:
  Rectangle(double w, double h) : width(w), height(h) {}
  double area() override {
    return width * height;
  }
};
C++

In this example, we have defined a Shape class with a virtual area() function. We then defined two derived classes, Circle and Rectangle, that override the area() function.

We can create Shape objects using pointers to the base class:

Shape* s1 = new Circle(5);
Shape* s2 = new Rectangle(3, 4);
C++

We can then call the area() function on these objects:

std::cout << "Circle area: " << s1->area() << std::endl;
std::cout << "Rectangle area: " << s2->area() << std::endl;
C++

This will output:

Circle area: 78.5
Rectangle area: 12

The correct area() function is called for each object based on its actual type. This is an example of polymorphism in action.

Conclusion

Object-oriented programming is a powerful paradigm that allows us to model complex systems using objects with data and behavior. C++ is a versatile programming language that supports many features of OOP, including classes, objects, encapsulation, inheritance, and polymorphism. By mastering these concepts, we can write clean, maintainable code that is easy to understand and extend.

xalgord
WRITTEN BY

xalgord

Constantly learning & adapting to new technologies. Passionate about solving complex problems with code. #programming #softwareengineering

Leave a Reply