c++类种类 创建&销毁 顺序

#include "line.h"
#include "stdlib.h"
/*
坐标类line包含2个点类
程序说明了类中类的创建销毁顺序
*/
void  main(){
line *p = new line(1, 2, 3, 4);
delete p;   
p = NULL;
system("pause");
}


Dot::Dot()  1 2
Dot::Dot()  3 4
line
~line()
Dot::~Dot()3 4
Dot::~Dot()1 2

class Dot{
public:
Dot(int a,int b);
~Dot();
int getX();
int getY();
void setX(int setx);
void setY(int sety);


private:
int x;
int y;
};



#include  "Dot.h"
class line{
public:
line(int x1, int y1, int x2, int y2);
~line();
void setA(int a, int b);
void setB(int a, int b);
void printinfo();
private:
Dot d1;
Dot d2;


};


#include "Dot.h"
#include <iostream>
using namespace std;
Dot::Dot(int a, int b) :x(a), y(b)
{
cout << "Dot::Dot()  " <<x<<" "<<y <<endl;
}
Dot::~Dot(){
cout << "Dot::~Dot()" << x << " " << y << endl;
}
int Dot::getX(){
return x;
}
int Dot::getY(){
return y;
}
void Dot::setX(int setx){
x = setx;
}
void Dot::setY(int sety){
y = sety;
}



#include "line.h"
#include "iostream"
using namespace std;
line::line(int x1, int y1, int x2, int y2):d1(x1,y1),d2(x2,y2){
cout << "line"<<endl;
}
line::~line(){
cout << "~line()" << endl;
}
void line::setA(int a, int b){
d1.setX(a)  ;
d1.setY(b) ;
}
void line::setB(int a, int b){
d2.setX(a) ;
d2.setY(b);
}
void line::printinfo(){
cout << "点D1 " << d1.getX() << d1.getY();
cout << "点D2 " << d2.getX() << d2.getY();


}


猜你喜欢

转载自blog.csdn.net/qq_18310233/article/details/78989725