C++ 带参数构造函数
我们知道,每当我们创建一个类时,会自动生成一个默认构造函数,在其他语言中,这个默认构造函数是隐藏的。但是当我们自己创建构造函数时,在主函数中创建对象时,我们可以自由地传递我们认为对构造函数来说是必要的参数。创建一个带参数的构造函数非常简单。我们只需要在类函数中添加必要的参数即可。
C++代码
// Here we are writing down the C++ Programming language code
// demonstrate the concept of Parameterized constructor in C++
#include
using namespace std;
// the below code snippet helps us with creating the
// Parameterized constructor class in C++ language
class ParamA {
private:
// we have declared that variables b and c in private mode are
int b, c;
public:
ParamA (int b1, int c1)
{
// we have declared that variables b and c in private mode are
// given the values here as b1 and c1 now
b = b1;
c = c1;
}
// this is the getter to get the value for variable b when it is
// called
int getX ()
{
return b;
}
// this is the getter to get the value for variable c when it is
// called
int getY ()
{
return c;
}
};
// the main driver code functionality starts from here
int main ()
{
ParamA p1(10, 15);
cout << "the value we have created for p1.b is: " << p1. getX() << ", the value we have created for p1.b is: p1.c = " << p1.getY();
return 0;
}
输出:
/ tm p/ po Rl 42 Ph db .o
the value we have created for p1.b is: 10, the value we have created for p1.b is: p1.c = 15
C++代码
// Here we are writing down the C++ Programming language code
// demonstrate the concept of Parameterized constructor in C++
#include
using namespace std;
// the below code snippet helps us with creating the
// Parameterized constructor class in C++ language
class ParamCode {
public:
int x;
ParamCode (int i);
~ParamCode ();
};
ParamCode::ParamCode (int i) {
x = i;
}
ParamCode::~ParamCode() {
cout<< "After destructing the objects which are having the value of x is as follows: " << x <<" \n";
}
// the main driver code functionality starts from here
int main () {
ParamCode t1(20);
ParamCode t2(15);
cout<< "the values for t1.x is: "<
输出:
/tmp/poRl42Phdb.o
the values for t1.x is: 20 the values for t2.x is: 15
After destructing the objects which have the value of x is as follows: 15
After destructing the objects which have the value of x is as follows: 20
C++代码
// Here we are writing down the C++ Programming language code
// demonstrate the concept of Parameterized constructor in C++
#include
using namespace std;
// the below class we are creating to calculate the length of the rectangle
class rectangle {
private:
double length;
double breadth;
public:
// to demonstrate our concept, we are creating parameterised constructor to calculate the length of the rectangle
rectangle(double len, double brt) {
// below code helps us with initialising the members of the private members of the class with certain variables
length = len;
breadth = brt;
}
double calculateArea() {
return length * breadth;
}
};
// the main driver code functionality starts from here
int main() {
// below code snippet we have written in C++ programming language helps us with creating the objects
// which are initialised with certain data members to make the object creation successful
rectangle rect1(840, 18.6);
rectangle rect2(628.5, 46);
// the below couple of cout statements help us print the values after the operation is performed successfully!
cout << "after calculating the area of rectangle 1 we have got as: " << rect1.calculateArea() << endl;
cout << "after calculating the area of rectangle 2 we have got as: "<< rect2.calculateArea() << endl;
return 0;
}
输出:
after calculating the area of rectangle 1, we have got as 15624
after estimating the size of rectangle two, we have obtained: 28911