In this lesson, you will learn
There are several types of constructors in C++
class Box {
public:
// Default constructor
Box() {
// Initialization code goes here
}
};
int main() {
Box obj; // Calls the default constructor
return 0;
}
#include
using namespace std;
//Declaring a class: Box
class Box {
double length,width,height;
public:
// Default constructor
Box() {
// Initialization code goes here
length=3.0;
width=4.0;
height=6.0;
}
double volume(){
return length*width*height;
}
};
int main() {
Box b1; // Calls the default constructor
double vol=b1.volume();
cout<<"Volume of Box b1: "<<vol;
return 0;
}
Volume of Box b1: 72
#include<iostream>
using namespace std;
//Declaring a class: Box
class Box {
double length, width, height; //Private by default
public:
/* Default constructor declaration
(implementation will be outside teh class)*/
Box();
double volume(){
return length*width*height;
}
};
// Default constructor definition
Box :: Box(){
length=4.0;
width=5.0;
height=7.0;
}
int main() {
Box b1; // Calls the default constructor
double vol=b1.volume();
cout<<"Volume of Box b1: "<<vol<<endl;
return 0;
}
Volume of Box b1: 72
crazyy…..and easy to understand content :>
You must be logged in to submit a review.