Opps
Opps
/*
Why OOPS?
*/
/* If the class has no attributes or properties then the size of the class is 1 byte only for identification
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 : properties can be accessed within the class but not outside the class.
3. protected : properties can be accessed within the class and inside the child class.
*/
/*
Getters and Setters in OOPS:
In OOPS getter is a function defined in a class through which we can access the values of
variables that we have
Setters are function 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 optimizing 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 allocation 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 function which gets invoked automatically 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 function what will happen is that when the argument is passed in the function 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 function used to deallocate the memory assigned to the class object.
Example:
~ConstructorName(){
In case of statically declared object the destructor will be called automatically but in case of
dynamically allocated
*/
/*
Static data member is a data member which can be accessed without the need to create a class
object.
Declaration Example:
class Name{
int main(){
Name obj;
*/
/*
Static functions:
Static function are functions that can be accessed without creating the class object.
It does not have this keyword as this keyword points to the current object and static functions does
not require an object to be
created.
Static functions can access only static data members of the class.
*/
/*
Friend functions:
A friend function is a function which can access the private data members of a class and usually
takes objects as input parameters.
A friend function's body is not delcared within the class and is not a member function of the class
meaning we can't call it
Delcaration 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 getter(){
cout<<"\nTest1: "<<test1<<endl;
cout<<"Test2: "<<test2<<endl;
cout<<"Test3: "<<test3;
};
};
int main(){
obj.setter(4, 5, 6);
obj.getter();
ChildTest t;
t.setter(4, 5, 6);
t.getter();