0% found this document useful (0 votes)
13 views2 pages

Class Struct: #Include #Include

Uploaded by

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

Class Struct: #Include #Include

Uploaded by

Rajendra Bhosale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Object-oriented programming (OOP) is a programming paradigm that revolves around the

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.

Polymorphism allows objects of different classes to be treated as objects of a common


superclass. This enables code to be written in a way that is more generic and flexible. C++
supports two types of polymorphism: compile-time polymorphism achieved through function
overloading and operator overloading, and runtime polymorphism achieved through virtual
functions and abstract classes.

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.

Let's consider a simple example of object-oriented programming in C++, a class representing a


Car:

#include <iostream>

#include <string>

class Car {

private:

std::string make;

std::string model;

int year;
public:

Car(std::string make, std::string model, int year)

: make(make), model(model), year(year) {}

void displayInfo() {

std::cout << "Make: " << make << std::endl;

std::cout << "Model: " << model << std::endl;

std::cout << "Year: " << year << std::endl;

};

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() {

Car myCar("Toyota", "Camry", 2022);

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.

You might also like