#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt) {
length = len;
height = hgt;
}
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// copy contents of wall1 to wall2
Wall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt) {
length = len;
height = hgt;
}
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// copy contents of wall1 to wall2
Wall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
In this program, we have used a copy constructor to copy the contents of one object of the Wall class to another. The code of the copy constructor is:
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
Notice that the parameter of this constructor has the address of an object of the Wall class.
We then assign the values of the variables of the obj object to the corresponding variables of the object calling the copy constructor. This is how the contents of the object are copied.
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
Notice that the parameter of this constructor has the address of an object of the Wall class.
We then assign the values of the variables of the obj object to the corresponding variables of the object calling the copy constructor. This is how the contents of the object are copied.
In main(), we then create two objects wall1 and wall2 and then copy the contents of wall1 to wall2:
// copy contents of wall1 to wall2
Wall wall2 = wall1;
Here, the wall2 object calls its copy constructor by passing the address of the wall1 object as its argument i.e. &obj = &wall1.
Note: A constructor is primarily used to initialize objects. They are also used to run a default code when an object is created.
// copy contents of wall1 to wall2
Wall wall2 = wall1;
Here, the wall2 object calls its copy constructor by passing the address of the wall1 object as its argument i.e. &obj = &wall1.
Note: A constructor is primarily used to initialize objects. They are also used to run a default code when an object is created.
Happy New Year friends.
Thank you for supporting me
Keep sharing and enjoy 2023
And yes we are coming with 5th sem notes soon
Thank you for supporting me
Keep sharing and enjoy 2023
And yes we are coming with 5th sem notes soon
If you want 5th sem BCA notes then you can join channel of 5th sem subjects.
We are provide notes in that channel very soon
We are provide notes in that channel very soon
Want more BCA 5sem notes 📝 to learn then join our others channels :-👇
~~
1⃣ Computer Network Refresher
▪️ https://t.me/+nJEUTkAttSQ4Zjg1
••••••••••••••••••••••••••••••••••••
2⃣ Computer Graphics And Animation Refresher
▪️ https://t.me/+_q9nXVbeDTcxOTRl
••••••••••••••••••••••••••••••••••••
3⃣ Advanced Computer Architecture Reafresher
▪️ https://t.me/+AKjQlj3uFrMwMDg1
••••••••••••••••••••••••••••••••••••
4⃣ Software Engineering Refresher
▪️ https://t.me/+Q6nfN9fcmfs2NjRl
••••••••••••••••••••••••••••••••••••
5⃣ Introduction To Internet Programming Refresher
▪️ https://t.me/+ZtKDH5KY1JZiM2M9
___
Join and Learn more.
Thankyou for joining us.
~~
1⃣ Computer Network Refresher
▪️ https://t.me/+nJEUTkAttSQ4Zjg1
••••••••••••••••••••••••••••••••••••
2⃣ Computer Graphics And Animation Refresher
▪️ https://t.me/+_q9nXVbeDTcxOTRl
••••••••••••••••••••••••••••••••••••
3⃣ Advanced Computer Architecture Reafresher
▪️ https://t.me/+AKjQlj3uFrMwMDg1
••••••••••••••••••••••••••••••••••••
4⃣ Software Engineering Refresher
▪️ https://t.me/+Q6nfN9fcmfs2NjRl
••••••••••••••••••••••••••••••••••••
5⃣ Introduction To Internet Programming Refresher
▪️ https://t.me/+ZtKDH5KY1JZiM2M9
___
Join and Learn more.
Thankyou for joining us.