OOPS basic concept in c++
OOPS
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or
methods that perform operations on the data, while object-oriented programming
is about creating objects that contain both data and methods.
Advantages of OOPS
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP makes it possible to create full reusable applications
with less code and shorter development time
Object-Oriented Programming is a methodology or paradigm to
design a program using classes and objects.
It simplifies software development and maintenance by
providing some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
C++ Classes/Objects
Classes in C++................(Collection of objects is
called class. It is a logical entity.)
Everything in c++ is associated with classes and objects,
along with its attributes and methods.
For example: in real life, a car is an object. The car has
attributes, such as weight and color, and methods, such as drive and brake.
A Class is like an object constructor, or a
"blueprint" for creating objects.
Object-Oriented Programming is a paradigm that provides many
concepts, such as inheritance, data binding, polymorphism, etc.
Objects in C++..................
Object means a real-world entity such as a pen, chair,
table, computer, watch, etc.
Inheritance in C++...................
The capability of a class to derive properties and
characteristics from another class is called Inheritance
Inheritance is one of the most important features of
Object-Oriented Programming.
Sub Class: The class that inherits properties from another
class is called Sub class or Derived Class
Super Class: The class whose properties are inherited by a
sub-class is called Base Class or Superclass.
When one object acquires all the properties and behaviors of
a parent object, it is known as inheritance.
It provides code reusability. It is used to achieve runtime
polymorphism.
It has many Types
Single Level Inheritance:
In single inheritance, a class is allowed to inherit from
only one class.
i.e. one base class is inherited by one derived class only.
Multilevel Inheritance:
In this type of inheritance, a derived class is created from
another derived class and that derived classcan be derived from a base class or
any other derived class. There can be any number of levels.
Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can
inherit from more than one class.
i.e one subclass is inherited from more than one base class.
Hierarchical Inheritance:
In this type of inheritance, more than one subclass is
inherited from a single base class.
i.e. more than one derived class is created from a single
base class.
Hybrid Inheritance:
Hybrid Inheritance is implemented by combining more than one
type of inheritance.
For example: Combining Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance in C++
Encapsulation in C++...............
Encapsulation is defined as wrapping up data and information
under a single unit.
In Object-Oriented Programming, Encapsulation is defined as
binding together the data and the
functions that manipulate them.
Encapsulation in C++.Encapsulation also leads to data
abstraction or data hiding. Using encapsulation also hides the data
Data Abstraction in C++...............
Data abstraction is one of the most essential and important
features of object-oriented
programming in C++. Abstraction means displaying only
essential information and hiding the
details. Data abstraction refers to providing only essential
information about the data to the
outside world, hiding the background details or
implementation.
Constructors/Deconstructor
A constructor is a special member function in C++ that is
automatically called when an object is created. Its primary purpose is to
initialize the object.
It has the same name as the class.
Constructors do not have a return type, not even void.
It is called automatically when an object is created.
A constructor with parameters to initialize objects with specific
values.
Syntax:
class MyClass { //
The class
public: // Access specifier
MyClass() { // Constructor
cout <<
"Hello World!";
}
};
It has three types
1. Default Constructors
2. Parameterized Constructor
3. Copy Constructor
Destructor
Destructor is an instance member function that is invoked
automatically whenever an object is going to
be destroyed. Meaning, a destructor is the last function that is going to be called before an object is destroyed.
In object-oriented programming, constructors are special
methods or functions that initialize an object when it is created.
They are used to set initial values for object attributes or
perform setup tasks necessary for the object. In most programming languages,
constructors have specific syntax and rules.
Here’s a breakdown of constructors and their types:
1. Default Constructor
A default constructor is a constructor that takes no
arguments or has no parameters. It initializes objects with default values.
Characteristics:
Automatically provided by the compiler if no other
constructor is defined (in some languages like C++).
Can also be explicitly defined by the programmer.
2. Parameterized Constructor
A parameterized constructor takes arguments to initialize
the object with specific values.
Characteristics:
Provides flexibility to set different initial values for
each object.
Overcomes the limitation of default constructors.
3. Copy Constructor
A copy constructor creates a new object as a copy of an
existing object. It duplicates the values of an existing object into the new
one.
Characteristics:
Usually used for deep copying or custom copying behavior.
Automatically provided by the compiler in languages like C++
unless explicitly defined.
Polymorphism in C++...............
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Like Inheritance lets us inherit attributes and methods from another class.
Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
For example, think of a base class called Animal that has a method called animalSound().
Derived classes of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation
of an animal sound (the pig oinks, and the cat meows, etc.):
polymorphism is of two types Compile time polymorphism and
run time polymorphism
Compile time polymorphism(Function overloading and operator
overloading)------------
Function overloading.........
When there are multiple functions with the same name but
different parameters,
then the functions are said to be overloaded, hence this is
known as Function Overloading.
Functions can be overloaded by changing the number of
arguments or/and changing the type of arguments.
operator Overloading.........
C++ has the ability to provide the operators with a special
meaning for a data type, this ability is known as operator overloading.
For example, we can make use of the addition operator (+)
for string class to concatenate two strings.
We know that the task of this operator is to add two
operands.
So a single operator ‘+’, when placed between integer
operands, adds them and when placed between string operands, concatenates them.
used to perform the operations on the user defined datatype
The advantage of operator overloading is to perform
different operations on the same operand
Runtime Polymorphism(Function overriding and virtual
functions)
Runtime polymorphism (also called dynamic polymorphism)
refers to the ability of a program to decide at runtime which method or
function to call.
In runtime polymorphism, the compiler resolves the object at
run time and then it
decides which function call should be associated with that
object.
It is also known as dynamic or late binding polymorphism.
( Function Overriding )
Function Overriding occurs when a derived class has a
definition for one of the member functions of the base class.
That base function is said to be overridden.
Virtual Functions......
A virtual function is a member function that is declared in
the base class using the keyword virtual
and is re-defined (Overridden) in the derived class. It
tells the compiler to perform late binding
(Late binding refers to the process of resolving function
calls at runtime instead of at compile time.)
where the compiler matches the object with the right called
function and executes it during the runtime.
This technique falls under Runtime Polymorphism.
This Pointer------------
In C++ programming, this is a keyword that refers to the
current instance of the class.
There can be 3 main usage of this keyword in C++.
It can be used to pass current object as a parameter to
another method.
It can be used to refer current class instance variable.
It can be used to declare indexers.
#include<iostream>
using namespace std;
class Test{
private:
int x;
public:
void setX (int x)
{
this->x = x;
}
void print() {
cout << "x
= " << x << endl;
}
};
int main()
{
Test obj;
obj.setX(56);
obj.print();
return 0;
}
Comments
Post a Comment