Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.
Objects are basic run-time entities in an object oriented system, objects are instances of a class these are defined user defined data types.
Example:
class Person {
char name[20];
int id;
public:
void getdetails(){}
};
int main(){
Person p1;
}
Here p1 is an object.
Class is a blueprint of data and functions or methods. Class does not take any space.Class is a user defined data type like structures and unions in C.
Syntax for class
class class_name {
private:
//data members and member functions declarations
public:
//data members and member functions declarations
protected:
//data members and member functions declarations
};
Combining data and functions into a single unit called class and the process is known as Encapsulation.Data encapsulation is important feature of a class. Class contains both data and functions.
it is the process by which object of one class aquire the properties or features of objects of another class. The concept of inheritance provide the idea of reusability means we can add additional features to an existing class without Modifying it.
A greek term means ability to take more than one form. An operation may exhibite different behaviours in different instances. The behaviour depends upon the types of data used in the operation.
Abstractions refer to the act of representing essential features without including background details or explanation. They are commonly known as Abstraction Data Type(ADT).
Exception handling is the process of responding to the occurrence, during computation, of exceptions – exceptional conditions requiring special processing – often changing the normal flow of program execution.
Template is simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write same code for different data types.
Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
There are three kinds of access modifiers. Public, Private and Protected.
All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.
Those function are decleared inside the class are called member function.
A function is a block of organized, reusable code that is used to perform a single, related action.
int add(int a, int b) {
int result;
result = a + b;
return result;
}
This is a function.
There are three kinds of function in OOP (Object oriented programming).
- Inline function
- Outline function
- Stand alone function
The function that decleared with body inside the class is called inline function.
class Animal {
void getAlnimal() {
int a, b;
}
};
It faster in execution.
It needs more memory space.
The function that decleared without body iside the class is called outline function.
class Animal {
voi show();
};
void Animal::show() {
int a, b;
}
It needs less memory space.
It is slow in execution.
A stand-alone function is just a normal function that is not a member of any class and is in a global namespace.
The function with the same name of class decleared inside the class is called constructor function. Constructor function is execute when object created.
class Student {
int student() {
int id;
char name[40];
}
};
Variable is a container to store data. Variable are differents type. Integer, character, float, double. differents types of variable store differents types data.
The varible which is decleared inside a function or as a function parameter is called local variable.
int call(int a){
char name;
}
In the following function a
is local variable as a function parameter and name
is also local variable decleared inside the body of the function.
The variable is decleared in outside of all function and it should be called any where of scope is called global variable.
int data;
int callIt() {
data = 10;
printf(data);
}
int do() {
data = 30;
printf(data);
}
In the following example we can say data
is the global variable. Because it is called from differets function.
The variable decleared inside the class is called member variable.
class Student {
public:
int data;
}
};
In the following example data
is a member variable.
When const
keyword is used to declear a function and the value of member variable can not be changed within the function is called constant member function.
When member variable can't be modified then we need to decleare constant member function.
class Beautiful {
public:
void see() const {
cout << "I can see beautiful nature";
}
}
In the following class, can see that the see()
function is constant member function.
In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime is the entire run of the program.
We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.
class Beautiful {
public:
int static count;
void show() {
cout << count;
}
};
int Beautiful::count;
int main() {
Beautiful B;
B.count;
}
class A {
public:
A(){
cout << "This is constructor function";
}
};
int main() {
A a;
}
Destructor is a member function which destructs or deletes an object.
When need to delete or destruct an object it should be decleared desctructor function.
class Test {
public:
~Test() {
cout << "This is destructor function";
}
A destructor function is called automatically when the object goes out of scope: (1) the function ends (2) the program ends (3) a block containing local variables ends (4) a delete operator is called
class A {
public:
void get_roll() {
cout << "This is inline funciton";
}
};
int main() {
A a;
a.get_roll();
}
class A {
public:
void show_something();
};
void A::show_something() {
cout << "This is outline function";
}
int main() {
A a;
a.show_something();
}
1. Non parameterized constructor
2. Parameterized Constructor
3. Copy Constructor
To initialize member variable, then constructor function should be decleare. If we need to autometically call any member function it should be called inside the constructor function.
When a function is decleared with same name of class and autometically called when object created of that class is called constructor function. The constructor function which is delceared without parameter is called non parameterized cosntructor function.
When it doesn't need to pass any value withing the object, then it should be decleared non parameterized constructor.
class A {
public:
A() {
}
};
int main() {
A a;
}
When a constructor function is decleared with parameter and when it is called it need to accept parameter value that is called parameterized constructor.
When we nedd to pass any value within the object, it should be decleared parameterized constructor.
- Initialize the member variable
- It is possible to change default value of constructor parameter
class A {
public:
A(int a, int b) {
cout <<a<<b<<endl;
}
};
int main() {
A a(10, 20);
}
A copy constructor is a member function which initializes an object using another object of the same class.
- When instantiating one object and initializing it with values from another object (as in the example above).
- When passing an object by value.
- When an object is returned from a function by value.
ClassName (ClassName &old_obj);
class Point {
private:
int x, y;
public:
Point(int x1, int y1) {
x = x1; y = y1;
}
// Copy constructor
Point(Point &p2) {
x = p2.x; y = p2.y;
}
};
int main()
{
Point p1(10, 15);
Point p2 = p1;
}
Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++.
#include <iostream>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}
The capability of a class to derive properties and characteristics from another class is called Inheritance.
To reduce the duplication of code then inheritance is the best.
The class that inherits properties from another class is called Sub class or Derived Class.
The class whose properties are inherited by sub class is called Base Class or Super class.
If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class.
If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class.
If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class.
Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one
// multiple inheritance
#include <iostream>
using namespace std;
// first base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler {
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}