Class Struct: #Include #Include
Class Struct: #Include #Include
concept of objects. These objects are instances of classes, which are templates for creating
objects. C++ is a powerful programming language that fully supports object-oriented
programming. In this paradigm, data and functions are bundled together into objects, allowing
for a modular and organized approach to programming.
At the heart of OOP in C++ are four key principles: encapsulation, inheritance, polymorphism,
and abstraction.
Encapsulation is the bundling of data and functions that operate on the data into a single unit,
typically a class. This unit hides the internal state of the object and only exposes a public
interface for interacting with it. This helps in preventing unauthorized access to data and ensures
that the object's state remains consistent.
Inheritance is the mechanism by which a class can inherit properties and behavior from another
class. This promotes code reuse and allows for the creation of hierarchies of classes. In C++,
classes can inherit from other classes using the class or struct keywords. Derived classes
inherit attributes and methods from their base classes and can override or extend them as needed.
Abstraction involves simplifying complex systems by modeling only the essential aspects while
hiding unnecessary details. In C++, abstraction is achieved through the use of classes and
objects. Classes define the abstract data type along with its properties and behaviors, while
objects are instances of these classes that represent specific instances of the abstract data type.
#include <iostream>
#include <string>
class Car {
private:
std::string make;
std::string model;
int year;
public:
void displayInfo() {
};
In this example, Car is a class that encapsulates the properties of a car (make, model, year) and a
method displayInfo() to display these properties. Objects of the Car class can be created to
represent specific cars and their information can be displayed using the displayInfo() method.
int main() {
myCar.displayInfo();
return 0;
Here, we create an object myCar of the Car class with specific make, model, and year. We then
call the displayInfo() method on myCar to display its information.
This example illustrates the principles of encapsulation, abstraction, and the creation of objects
in C++ using classes. Object-oriented programming in C++ provides a powerful and flexible way
to model real-world entities and systems, making it a popular choice for developing a wide range
of applications.