构造函数
#include <iostream>
using namespace std;
class CTestA
{
public:
CTestA(int a, int b)
{
x = a;
y = b;
cout << "x=" << x << ",y=" << y << endl << endl;
}
CTestA(int a)
{
cout << "a=" << a << endl;
}
int sumxy()
{
return (x + y);
}
private:
double x, y;
};
int main()
{
CTestA obja(56, 90);
cout << "x+y=" << obja.sumxy() << endl;
CTestA objb(30000);
return 0;
}
#include <iostream>
using namespace std;
class CTestA
{
public:
CTestA()
{
x = 0, y = 0;
cout << "\n初始化局部静态对象." << endl;
}
CTestA(double a)
{
x = a;
y = 0;
cout << "初始化全局对象." << endl;
}
CTestA(double a,double b)
{
x = a;
y = b;
cout << "初始化局部对象." << endl;
}
private:
double x, y;
};
CTestA objb(88.8);
void FuncTest()
{
cout << "\n程序开始进入FuncTest()函数.\n\n";
CTestA objc(10, 20);
static CTestA objd;
}
int main()
{
cout << "\n程序开始执行-->main()函数\n\n";
CTestA obja(4.5, 6.5);
FuncTest();
return 0;
}
#include <iostream>
using namespace std;
class CTestA
{
public:
CTestA()
{
cout << "调用缺省构造函数.\n\n";
}
void setxy(int a, int b)
{
x = a;
y = b;
}
void disp()
{
cout << "x=" << x << ",y=" << y << endl;
}
private:
double x, y;
};
int main()
{
CTestA obja, objb;
obja.setxy(40, 90);
cout << "obja对象结果为:\n";
obja.disp();
cout << "objb对象结果为:\n";
objb.disp();
return 0;
}
#include <iostream>
using namespace std;
class CTestA
{
public:
CTestA()
{
x = 0;
y = 0;
cout << "调用缺省构造函数" << endl;
}
CTestA(double a, double b)
{
x = a;
y = b;
cout << "调用带参构造函数" << endl;
}
void dispxy()
{
cout << "x=" << x << ",y=" << y << endl << endl;
}
private:
double x, y;
};
int main()
{
CTestA* pobja, * pobjb;
pobja = new CTestA;
pobjb = new CTestA(200, 800);
pobja->dispxy();
pobjb->dispxy();
delete pobja;
delete pobjb;
return 0;
}
析构函数
#include <iostream>
using namespace std;
class CTestA
{
public:
CTestA()
{
cout << "调用缺省构造函数" << endl;
}
CTestA(double a, double b)
{
x = a;
y = b;
cout << "调用带参构造函数" << endl;
}
~CTestA()
{
cout << "调用析构函数" << endl;
}
private:
double x, y;
};
int main()
{
CTestA obj1;
CTestA obj2(5.6,70.8);
CTestA obj3(10, 20);
return 0;
}
#include <iostream>
using namespace std;
class CTestA
{
public:
CTestA()
{
cout << "\n调用构造函数CTestA::CTestA()." << endl;
pc = new int(10000);
cout << *pc << endl;
}
~CTestA()
{
delete pc;
cout << "调用析构函数~CTestA::CTestA()." << endl;
}
private:
int* pc;
};
int main()
{
CTestA* p1 = new CTestA;
CTestA* p2 = (CTestA*)malloc(sizeof(CTestA));
delete p1;
free(p2);
return 0;
}
#include <iostream>
using namespace std;
class CTestA
{
public:
CTestA(double a, double b)
{
x = a;
y = b;
cout << "调用带参构造函数" << endl;
}
~CTestA()
{
cout << "调用析构函数" << endl;
}
void dispxy()
{
cout << x << "," << y << endl << endl;
}
private:
double x, y;
};
int main()
{
CTestA obj1(2, 3);
obj1.dispxy();
obj1 = CTestA(90, 20);
obj1.dispxy();
return 0;
}
拷贝构造函数
#include <iostream>
using namespace std;
class CTestA
{
private:
int* ptr;
public:
CTestA(int a);
CTestA(const CTestA& obj);
~CTestA();
int getptr()
{
return *ptr;
}
};
CTestA::CTestA(int a)
{
cout << "调用构造函数." << endl;
ptr = new int;
*ptr = a;
}
CTestA::CTestA(const CTestA& obj)
{
cout << "调用拷贝构造函数并为指针ptr分配内存空间" << endl;
ptr = new int;
*ptr = *obj.ptr;
}
CTestA::~CTestA()
{
cout << "释放内存空间." << endl;
delete ptr;
}
void disptr(CTestA obj)
{
cout << "ptr的值为:" << obj.getptr() << endl;
}
int main()
{
CTestA obj(5600);
disptr(obj);
return 0;
}