OOP Notes (cpp)
OOP Notes (cpp)
------
it is another way of writing code , if we use opps to implement logic rather than
using
simple c++ it makes easier to implement real life and practical senerios in our
code
why important
-------------
it helps to improve code organization and maintainability, and can save time by
allowing developers to reuse code
--------------------------------
method - is a function written inside the class or functions which are made for
classes and objects are methods
just make class one time - and use that again and again to make objects - that why
opps increases reusability
ex - teachers example is important
like give blue print one time toyate will use that to manufacture the different car
again n again using that one blueprint(classes)\
also in teacher(object) ex - only one we have to say what are its properties and
methods and then we can create may teachers profile together instead of creating
lots of variables
so if we have to use entites which are close to real world and it has many
properties and method , there will use classes and objects
strings
--------
A string is a variable that stores a sequence of letters or other characters, such
as "Hello" or "May 10th is my birthday!
-----------------------------------------------------------------------------------
------
constrctor
----------
if we want that one value of data member/attribute is set and its doesn't change we
can use constructor
like in teacher , we can use dep = computer , so everytime objected is created ,
automatically dept will be computer
we don't have to write t1.dept = "computer";
memory allocation is for objects when they are created not for classes , when call
is going to constructor
types of constructor:-
1 - non parameterized consss
2 - parameterized function
3 - copy constructor
constructor overloading - when there more than one constructor with same name , but
they have diff number of parameters
it is allowed
but when we are doing dynamic memory allocation , the issue is coming in shwllow
copy
destructor
------------------
it is also automatically called by compiler
opp of constrctor
~teacher(){
cout<<"i am deleting";
}
--> A destructor is a member function that is invoked automatically when the object
goes out of scope
use to destroye a object
Inheritance
-----------
It is used for code reusability
ex - let class A(parent class , base class) has one property , so why to make it
again for other classes
we will directly inherit to our class B(child , derived class)
companies has very big classes which has many properties , so to write code for
properties again and again it not suitable
--> if we have any private member or info in parent or base class it will never
inherits to the child class
let suppose we have private properties that we want to inherit , we dont want to
give access just want to inherit it
so we can make it protected;
so that it's derived class can also access those properties;
example, in a system for modeling animals, you can have a base class, Animal, and
derived classes like Dog, Cat, and Bird,
ex - let take example of bank , we can make account as parent class and then we can
make coustomer as a derived class and we can inherit the properties like name ,
id , password;
Polymorphism
-----------------
ex - constructor overloading
types:-
------
1 - compile time poly - means during compile time only we know the output
ex - constructor overloading
ex - function overloading - is an compile time poly , when we have function with
same name but have diff types or number of parameters
ex - operator overloading - same operator we are using in diff forms
ex - virtual function
if we are writing virtual infront of any function it will become virtual function
any function can be made virtual function;
Abstraction
-------------
whenever we use access modifiers in our class that means we are using abstraction
also it not only hide sentive and uncessary info but also responsible for showing
important info also
Encapsulation - only hides the data . but Abstraction hides + shows the necceasary
info also
};
abstract class never creates any objects
it becomes the blueprint of other classes that are going to be implemented
it is only made for inheritance