« Oops in C++ And constructors, destructors, copy constructor and copy assignment operator

How to define class in C++

1class Student
2{
3public:
4 int rollNumber; // by default properties are non-static
5 int age;
6}

Object creating using static allocation

1Student s1;

Object creating using dynamic allocation

1Student *s10 = new Student();
2cout << "s10->age:" << s10->age << endl;

dynamic_memory_allocation.jpg

How to include a different C++ file into your C++ file

1#include "Student.cpp"

By writing this line our code in Student file gets copied into our current file.

Accessing class variables in static and dynamic allocation both

1s1.age = 24;
2s1.rollNumber = 5;
3(*s10).age = 54;
4(*s10).rollNumber = 154;

Access modifiers

These decide how far our properties and functions are accessible. There are mainly three different access modifiers : private, protected, public

private properties or functions are accessible only within the class.

public properties or functions are accessible everywhere.

If we don't mention then all properties and functions are by default private

Everything mentioned under a access modifier has that access modifier until unless we change the access modifier.

Getter and setter

we use getter and setter for accessing and setting private properties and functions.

1int getAge()
2{
3 return age;
4}
5
6void setAge(int a, int password)
7{
8 if (password != 123)
9 {
10 return;
11 }
12 if (a < 0)
13 {
14 return;
15 }
16 age = a;
17}

By using getter and setter we can add logic while accessing and editing properties.

Default constructor

When we create a object Student s1. It internally actually calls s1.Student(). This special function is actually called constructor.

  1. Same name as class.
  2. No return type.
  3. No input arguments

If we don't create this constructor still this exists. As soon as we create class this constructor is automatically created for us.

We can create our own constructor also.

Default constructor structure is something like below:

1Student(){
2
3}

For every object constructor is called. Constructor primary function is to intialise the variables with default values which are actually garbage values.

For this lifetime of an object constructor will be called only once at the time of the creation.

As soon as we create our own constructor default constructor is not available for us anymore.

this keyword

this holds the reference to the current object

value of this and &object will come out to be same.

As this holds address of the current object so it is a pointer.

Copy constructor

It creates and object which is copy of another object.

1Student s2(s1)

s2 will have all the properties same as s1. Student s2(s1) => s2.Student(s1) , but we didn't created any such constructor. So when we create object copy constructor is created for us.

Again as this is a constructor it will be called only once at the time of object creation.

1Student s11(10, 1001);
2cout << "s11: ";
3s11.display2();
4
5Student s12(s11);
6cout << "s12: ";
7s11.display2();
8
9Student *s13 = new Student(20, 2001);
10cout << "s13: ";
11s13->display2();
12
13Student s14(*s13);
14cout << "s14: ";
15s14.display2();
16
17Student *s15 = new Student(*s13);
18cout << "s15: ";
19s15->display2();
20
21Student *s16 = new Student(s11);
22cout << "s16: ";
23s16->display2();

Copy assignment operator(=)

1s2 = s1

Here all the properties of the s1 will be copied into the properties of s2 one by one.

1Student s18(20, 2001);
2Student s19(10, 1001);
3Student *s20 = new Student(30, 001);
4s19 = s18;
5cout << "s19: ";
6s19.display2();
7*s20 = s18;
8cout << "s20: ";
9s20->display2();
10s20->age = 67;
11s20->rollNumber = 890;
12s19 = *s20;
13cout << "s19: ";
14s19.display2();

Destructor

Its function is to de-allocate our objects memory.

  1. It has same name as out class.
  2. No Return type.
  3. No input arguments.

To differentiate between default constructor and destructor we place ~ tilda before the destructor.

This also gets created with object creation.

This gets called only once in lifetime of the object. It gets called when our object is about to be destroyed.

We can create our own destructor but we can't have multiple destructors like constructor. There can be only one destructor.

As soon as we create our own destructor default destructor won't be accessible to us.

Before moving out of our main function destructors for our objects will get called.

Destructors for statically allocated objects will get called automatically but for dynamic objects it won't get called. For dynamic objects we only have pointer in stack to object in heap . To de-allocate pointer we don't need destructor.

1Student s1(10, 1001);
2Student s1(20, 2001);
3Student *s3 = new Student(30, 3001);
4delete s3;

destructors.jpg

Special case

If we try to use copy assignment operator at the time of object creation then copy constructor gets called under the hood to optimize.

1Student s1(); // constructor 1 called
2Student s1(20, 2001); // constructor 2 called
3Student s4(s3); // copy constructor
4s1 = s2; // copy assignment operator
5Student s5 = s4; // copy constructor rather than copy assignment operator

special_case.jpg

For full code please use

https://github.com/anishakd4/ds/tree/master/cn/oops