OOPs concept in C++
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 this function.
Class:-
It is a user defined data types. which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.
Object :-
When a class is defined no memory is allocated but when it is instantiated (i.e. object is created) memory is allocated.
Encapsulation :-
In oops, Encapsulation is defined as binding together the data and the functions that manipulates them.
Abstraction :-
Abstraction is a process to show only useful data and remaining hide from user. it means displaying only essential information and hiding the details.
- Abstraction using classses.
- Abstraction using header files ( math.h => pow())
- Operator overloading
- function overloading ( int sum (10, 20, 30) )
- Subclass
- SuperClass
- Reusability
- Default constructor (no parameter passed )
- Parametrized constructor
- copy constructor.
- Public - can be accessed by any class.
- Private - can be accessed only by a function in a class ( inaccessible outside the class )
- Protected - can be accessed by subclass or that class.
- Operator overloading
- Function overloading
- Avoid code duplication and inc reusability.
- Can change internal implementation of class independently.
- try : represent a block of code that can throw an exception.
- catch : represent a block of cade that get executed when error is throw.
- throw : used to throw an exception.
C | C++ |
C supports procedural prog. | c++ is known as hybrid language, becouse it support both procedural and object oriented programming |
As c does not support the oops concept so it has no support for poymorphism, encapsulation and inheritance | C++ has upper for polymorphism, encapsulation and inheritance as it is an oops language. |
C is a subset of c++ | C++ is superset of C |
C contains 32 keywords | C++ contain 52 keywords ( public, private, protected, try , catch, throw... |
C is function driven language | C++ is object driven language |
Function and operator overloading is not support in c | C++ supports function operator overloading |
C does not support exception handling | C++ supports exception handling using try and catch |
- Static Variable in a function :
- Static variable in a class:
- Declared inside the class body.
- Also known as class member variable.
- Static variable closen't belong to any object, but to te whole class.
- There will be any are copy of static member variable for the whole class.
class Accout{private :int balance;static float roi;public:void setBalance ( int b){balance = b;}};
Object can also be declared as staticclass Accout{private :int balance;static float roi;public:void setBalance ( int b){balance = b;}};
- Constructors is an special member function of the class. It is automatically invoked when an object is created.
- it has no return type
- Constructor as same name as class itself
- It we do not specify then c++ compiler generates a default constructor for us.
- Default constructor
- copy Constructor
- But it any of the constructor is created by user then default constructor. will not be created by compiler.
- construction overloading can be done just like function overloading.
Deep Copy is a possible only with user defined constructor. In user defined copy constructor, we make some that pointers of copied object points to new memory location.
- Destructor is a member function which destructs or deletes an object.
- Destructor don't take any argument and don't have any return type.
- only one destructor is possible.
- Destructor can not be static.
- Actually destructor doesn't destroy object it is the last function that invoked before 0bject destroy.
- Destructor is used so that before deletion of object we can free space allocated for this resource. B/C it object gets deleted then space allocated for object will be free but resource doesn't.
class Complex { complex operator + ( Complex &c1 ) { complex res; res.a = c1.a; res.b = c2.b; } } int main c1 { c = c1 + c2 }As '+' can't add complex no's directly. so we can define a function with name + ,but we need write operator keyword before it so, we can use all operator like this.
Post a Comment