C++ Module1 Notes
C++ Module1 Notes
The programming paradigm is the way of writing computer programs. There are four
programming paradigms and they are as follows.
C++ OVERVIEW
Object Oriented Programming Concepts in
C++
There are some basic concepts that act as the building blocks of OOPs i.e.
1. Class
2. Objects
3. Encapsulation
4. Abstraction
5. Polymorphism
6. Inheritance
7. Dynamic Binding
8. Message Passing
Being an object-oriented programming language, C++ uses objects to
model real-world problems
C++ Class
A class is a blueprint for the object.
We can think of a class as the technical design (prototype) of a car. It
contains all the details about the brand, model, mileage, etc. We can then
build different cars based on these descriptions. Here, each distinct car is
an object.
An example for this can be:
class Car {
public:
// class data
string brand, model;
int mileage = 0;
// class function
void drive(int distance) {
mileage += distance;
}
};
In the above code, we have used the class keyword to create a class
named Car . Here,
brand and model are class attributes used to store data
drive() is a class function used to perform some operation.
C++ Objects
An object is an instance of a class.
For example, the Car class defines the model, brand, and mileage. Now,
based on the definition, we can create objects like
Car suv;
Car sedan;
Car van;
Here, suv , sedan , and van are objects of the Car class. Hence, the basic
syntax for creating objects is:
Class_Name object_name;
class Car {
public:
// class data
string brand, model;
int mileage = 0;
return 0;
}
Run Code
Output
Brand: Honda
Model: Accord
Distance driven: 50 miles
In this program, we have created a class Car with data members and a
member function. Also, we have created an object my_car of the Car class.
Notice that we have used the dot operator . with the my_car object in order
to access the class members.
my_car.brand = "Honda";
my_car.model = "Accord";
my_car.drive(50);
my_car.show_data();
1. C++ Encapsulation
In C++, object-oriented programming allows us to bundle together data
members (such as variables, arrays, etc.) and its related functions into a
single entity. This programming feature is known as encapsulation.
Encapsulation in C++
class Car {
public:
// class data
string brand;
string model;
int mileage = 0;
// class function
void show_data() {
// code
}
};
In C++, we hide data using the private and protected keywords. In contrast,
using the public keyword for certain class members makes those members
accessible to all other functions and classes.
2. C++ Abstraction
In object-oriented programming, abstraction refers to the concept of
showing only the necessary information to the user i.e. hiding the complex
details of program implementation and execution.
For example, let us consider a slightly modified version of the Car class:
class Car {
private:
// class data
int speed;
// class function
void show_car_status() {
// code
}
};
Suppose we want the show_car_status() function to show the status of the car
based on the value of the speed variable.
We can implement such conditional statements using
the if...else statement inside the show_car_status() function.
void show_car_status() {
if (speed != 0)
cout << "The car is being driven." << endl;
else
cout << "The car is stationary." << endl;
}
Here, we do not need to show the user all the codes we have written to
determine if the car is stationary or not; we just need to show them whether
the car is being driven or not depending on its speed .
In other words, we are only giving useful and relevant information to the
user, while hiding all the unnecessary details.
3. C++ Inheritance
Inheritance in C++ allows us to create a new class (derived class) from an
existing class (base class).
The derived class inherits features from the base class and can have
additional features of its own.
// base class
class Vehicle {
public:
string brand;
void show_brand() {
cout << "Brand: " << brand << endl;
}
};
// derived class
class Car : public Vehicle {
public:
string model;
void show_model() {
cout << "Model: " << model << endl;
}
};
int main() {
return 0;
}
Run Code
Output
Brand: Honda
Model: Accord
Here,
#include <iostream>
using namespace std;
// base class
class Shape {
public:
};
// derived class
class Square : public Shape {
public:
};
int main() {
return 0;
}
Run Code
Output
Shape
Square
Here,