Opps First
Opps First
/*
Why OOPS?
*/
/* If the class has no a ributes or proper es then the size of the class is 1 byte only for iden fica on
or tracking the class object
class test{
//Empty class
};
*/
/*
We can define classes in one file and use that class as a header file in another code by storing the
class file in the same folder
as the code in which we want to use the class and then #include "classfile-name.cpp".
*/
/*
Access Modifiers:
2. private : proper es can be accessed within the class but not outside the class.
3. protected : proper es can be accessed within the class and inside the child class.
*/
/*
Ge ers and Se ers in OOPS:
In OOPS ge er is a func on defined in a class through which we can access the values of
variables that we have
Se ers are func on that we create to assign or access values of those private variables in the
class.
*/
/*
Padding in Object-Oriented Programming (OOPS) refers to the process of adding extra bytes or
empty spaces within a structure or
class to ensure that it's data members are aligned to specific boundaries in memory. This
alignment is crucial for op mizing memory
access and improving the efficiency of the processor when fetching data.
The alignment of data members in memory is determined by the processor's architecture. For
example, a 32-bit processor typically
fetches data in chunks of 4 bytes, while a 64-bit processor fetches data in chunks of 8 bytes. To
ensure efficient data access,
the compiler inserts padding bytes between data members to align them on appropriate
boundaries.
*/
/*
In dynamic memory alloca on for objects, when we try to access the members of the class we can
access them like this,
(*objName).memberName
OR
*/
/*
Constructor in OOPS:
Constructor in a class is a special func on which gets invoked automa cally when the object of
that class is created.
*/
/*
This Keyword:
this keyword is used to denote the variables of the same class. It is basically a pointer that points
to the class variables.
For example:
class Example{
int data;
public:
this.data = data;
OR
};
In this func on what will happen is that when the argument is passed in the func on then the
member variable of the class
will be updated with the given value. If we do not use the this keyword then the value in the
class will not be updated and
/*
Example:
ClassName obj2(obj1); In this object what we did is copied all the members values of the obj1 in
obj2.
OR
*/
/*
Default copy contructor does shallow copy that means same memory will be accessed by the
copied object and if we change the
value of one member in the 1st object it will affect the value of the 2nd object.
Deep Copy:
In deep copy we can create copy without using the same memory.
varName = obj.val;
*/
/*
Destructor in OOPS:
Destructor is a special func on used to deallocate the memory assigned to the class object.
Example:
~ConstructorName(){
In case of sta cally declared object the destructor will be called automa cally but in case of
dynamically allocated
*/
/*
Sta c data member is a data member which can be accessed without the need to create a class
object.
Declara on Example:
class Name{
int main(){
Name obj;
*/
/*
Sta c func on are func ons that can be accessed without crea ng the class object.
It does not have this keyword as this keyword points to the current object and sta c func ons does
not require an object to be
created.
Sta c func ons can access only sta c data members of the class.
*/
/*
A friend func on is a func on which can access the private data members of a class and usually
takes objects as input parameters.
A friend func on's body is not delcared within the class and is not a member func on of the class
meaning we can't call it
Delcara on in class:
//body
*/
// #include<iostream>
// class test{
// };
// int main(){
// test t;
// cout<<sizeof(t);
// }
class TestClass{
private:
int test1;
int test2;
int test3;
public:
TestClass(){
test1 = this->test1;
test2 = this->test2;
test3 = this->test3;
test1 = val1;
test2 = val2;
test3 = val3;
void ge er(){
cout<<"\nTest1: "<<test1<<endl;
cout<<"Test2: "<<test2<<endl;
cout<<"Test3: "<<test3;
};
};
int main(){
obj.ge er();
ChildTest t;
t.ge er();