OOPs Definition
OOPs Definition
Definition
The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function.
Class
A class is a logical entity used to define a new data type. A class is a user-
defined type that describes what a particular kind of object will look like.
Thus, a class is a template or blueprint for an object. A class contains
variables, methods, and constructors.
The car class would allow the programmer to store similar information
unique to each car (different models, maybe different colors, etc.) and
associate the appropriate information with each car.
Object
You need to have a class before you can create an object. When a class is
defined, no memory is allocated, but memory is allocated when it is
instantiated (i.e., an object is created).
Here,
objectName: It is the name of the object created by class_name.
The class’s default constructor is called, and it dynamically allocates
memory for one object of the class. The address of the memory allocated
is assigned to the pointer, i.e., objectName.
Features of OOPs:-
PrevNext
Realworld class modeling example:-
// creating Animal
class Animal {
bool gives_birth;
bool lay_egg;
bool live_in_ground;
bool live_in_water;
bool have_wings;
};
int main() {
// creating an object of animal class
Animal mammal;
// assign values to instance variables
mammal.gives_birth = true;
mammal.lay_egg = false;
mammal.live_in_ground = true;
mammal.live_in_water = false;
mammal.have_wings = false;
Animal amphibian;
amphibian.gives_birth = false;
amphibian.lay_egg = true;
amphibian.live_in_ground = true;
amphibian.live_in_water = true;
amphibian.have_wings = false;
Animal bird;
bird.gives_birth = false;
bird.lay_egg = true;
bird.live_in_ground = true;
bird.live_in_water = false;
bird.have_wings = true;
}
We all know animals are a creature of God, every animal is different from
each other, but they also possess some unique properties. Here we create
a class Animal and define some animal characters (properties) that may
be shared for different kinds of animals.
We defined all the properties for each object, like whether they give birth
or not, whether they live in water, etc.
Here, Animal class provides a template or blueprint for creating objects
(mammal, bird, and amphibian).
Class
A class is a logical entity used to define a new data type. A class is a user-
defined type that describes what a particular kind of object will look like. A
class contains variables(data members), methods, and constructors.
Data Members:- The variables which are declared in any class by using
any fundamental data types (like int, char, float, etc.) or derived data
types (like class, structure, pointer, etc.) are known as Data Members.
Here,
class: class keyword is used to create a class in C++.
class_name: The name of the class.
class body: Curly braces surround the class body.
After closing curly braces, a semicolon(;) is used.
string model;
int year_of_manufacture;
bool _5g_supported;
// Constructor
smartphone(string mod, int manu, bool _5g_supp)
{
// Initializing data members
model = mod;
year_of_manufacture = manu;
_5g_supported = _5g_supp;
}
// Methods
void print_details()
{
cout << "Model : " << model << endl;
cout << "Year of Manufacture : " << year_of_manufacture << endl;
cout << "5g Supported : " << _5g_supported << endl;
}
};
PrevNext
Object
Object
Here,
objectName: It is the name of the object created by class_name.
We have created a smartphone class earlier in the class module, and Now
we will use that same class to make objects.
#include <iostream>
using namespace std;
// creating class
class smartphone
{
// class body
// Data Members (Properties)
string model;
int year_of_manufacture;
bool _5g_supported;
// Constructor
public:
smartphone(string model_string, int manufacture, bool _5g_)
{
// initializing data members
model = model_string;
year_of_manufacture = manufacture;
_5g_supported = _5g_;
}
// methods
void print_details()
{
cout << "Model : " << model << endl;
cout << "Year of Manufacture : " << year_of_manufacture << endl;
cout << "5g Supported : " << _5g_supported << endl;
}
};
int main()
{
// creating objects of smartphone class
smartphone iphone("iphone 11", 2019, false);
smartphone redmi("redmi note 11 t", 2021, true);
smartphone oneplus("oneplus nord", 2020, true);
return 0;
}
You need to have a class before you can create an object. When a class is
defined, no memory is allocated, but memory is allocated when it is
instantiated (i.e., an object is created)