0% found this document useful (0 votes)
5 views

Opps First

Uploaded by

21070085
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Opps First

Uploaded by

21070085
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include<iostream>

using namespace std;

/*

Why OOPS?

Readability, Reuseability, Extensibility, Features.

*/

/* 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".

Now we can create the object of that class.

*/

/*

Access Modifiers:

1. public : proper es can be accesses within and outside the class.

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

in the class as private.

Se ers are func on that we create to assign or access values of those private variables in the
class.

*/

/*

There is a concept of padding and greedy alloca on in OOPS:

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.

*/

/*

Dynamic memory alloca on of objects can be done like this

ClassName *objName = new ClassName();

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

objName -> memberName

*/
/*

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.

Name of contructor is same as that of class.

*/

/*

This Keyword:

this is basically a pointer to the object.

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:

void setFunc on(int data){

this.data = data;

OR

this -> data = data;

};

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

there will be no effect of the passed value.


*/

/*

Copy constructor in OOPS:

Example:

ClassName obj1(2, 2);

ClassName obj2(obj1); In this object what we did is copied all the members values of the obj1 in
obj2.

OR

ClassName obj2 = obj1;

*/

/*

Shallow Copy in copy constructor:

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.

Example of func on to be wri en:

ClassName( ClassName& obj){

dataType varName = new dataType;

varName = obj.val;

this -> val = varName;

*/

/*

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

object we have to call it manually by using the delete keyword.

*/

/*

Sta c Data Member:

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{

sta c int dataMember;

int Name::dataMember = val;

int main(){

cout<<Name::dataMember; Good prac ce

Name obj;

cout<<obj.dataMember; Bad prac ce

*/

/*

Sta c func ons:

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.

*/

/*

Friend func ons:

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

through the object of the class.

Delcara on in class:

friend return-type friend-func on-name(base-class-name &object-name);

outside the class:

return-type friend-func on-name(baseclass-name &object-name){

//body

*/

// #include<iostream>

// using namespace std;

// class test{

// };

// int main(){

// test t;

// cout<<sizeof(t);

// }
class TestClass{

private:

int test1;

int test2;

int test3;

public:

TestClass(){

TestClass(int test1, int test2, int test3){

test1 = this->test1;

test2 = this->test2;

test3 = this->test3;

void se er(int val1, int val2, int val3){

test1 = val1;

test2 = val2;

test3 = val3;

void ge er(){

cout<<"\nTest1: "<<test1<<endl;

cout<<"Test2: "<<test2<<endl;

cout<<"Test3: "<<test3;

};

class ChildTest: public TestClass{


int var1=5;

};

int main(){

TestClass obj(1, 2, 3);

obj.se er(4, 5, 6);

obj.ge er();

ChildTest t;

t.se er(4, 5, 6);

t.ge er();

You might also like