Teacher.h
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string name="Jim",int age=1,int m=100);//自定义有参数默认构造函数
void setName(string name);
string getName();
void setAge(int age);
int getAge();
int getMax();//定义能取到getMax的方法
private:
string m_strName;
int m_iAge;
const int m_iMax;//老师最多能带多少学生;是一个常量
};
Teacher.cpp
#include"Teacher.h"
Teacher::Teacher(string name, int age, int m) :m_strName(name), m_iAge(age) ,m_iMax(m) //**初始化列表初始化数据,iMax若使用普通的初始化会出现问题,const**
{
cout << "Teacher(string name,int age)" << endl;
}
int Teacher::getMax()
{
return m_iMax;
}
void Teahcer::setName(string name)
{
m_strName = name;
}
string Teahcer::getName()
{
return m_strName;
}
void Teacher::setAge(int age)
{
m_iAge = age;
}
int Teacher::getAge()
{
return m_iAge;
}
demo.cpp
#include<iostream>
#include<stdlib.h>
#include<string>
#include"Teacher.h"
int main(void)
{
Teacher t1;//Teacher t1("Merry",12,150);替换后结果发生改变
cout << t1.getName() << " " << t1.getAge() << " "<< t1.getMax()<<endl;
system("pause");
return 0;
}
Teacher.h
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string name="Jim",int age=1);//自定义有参数默认构造函数
Teacher(const Teacher&tea);//定义一个拷贝构造函数
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
string m_strName;
int m_iAge;
};
Teacher.cpp
#include"Teacher.h"
Teacher::Teacher(string name, int age, int m) :m_strName(name), m_iAge(age)
{
cout << "Teacher(string name,int age)" << endl;
}
Teacher::Teacher(const Teacher&tea)
{
cout << "Teacher(const Teacher&tea)" << endl;//拷贝构造函数
}
void Teahcer::setName(string name)
{
m_strName = name;
}
string Teahcer::getName()
{
return m_strName;
}
void Teacher::setAge(int age)
{
m_iAge = age;
}
int Teacher::getAge()
{
return m_iAge;
}
demo.cpp
#include<iostream>
#include<stdlib.h>
#include<string>
#include"Teacher.h"
int main(void)
{
Teacher t1;//实例化一个对象t1
Teacher t2=t1;//用t1对t2赋值的方式对t2进行实例化,调用的是拷贝构造函数
Teacher t3(t1);//调用拷贝构造函数
system("pause");
return 0;
}
运行结果
调用拷贝构造函数,只改变demo.cpp
#include<iostream>
#include<stdlib.h>
#include<string>
#include"Teacher.h"
void test(Teacher t)//参数传递的例子
{
}
int main(void)
{
Teacher t1;//实例化一个对象t1
test(t1);//调用拷贝构造函数
system("pause");
return 0;
}
Teacher.h
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string name="Jim",int age=1);//自定义有参数默认构造函数
Teacher(const Teacher&tea);//定义一个拷贝构造函数
~Teacher()//定义析构函数
void setName(string name);
string getName();
void setAge(int age);
int getAge();
int getMax();//定义能取到getMax的方法
private:
string m_strName;
int m_iAge;
const int m_iMax;//老师最多能带多少学生;是一个常量
};
Teacher.cpp
#include"Teacher.h"
Teacher::Teacher(string name, int age, int m) :m_strName(name), m_iAge(age)
{
cout << "Teacher(string name,int age)" << endl;
}
Teacher::Teacher(const Teacher&tea)
{
cout << "Teacher(const Teacher&tea)" << endl;//拷贝构造函数
}
Teacher::~Teacher()//定义析构函数
{
cout << "~Teacher() " << endl;
}
void Teahcer::setName(string name)
{
m_strName = name;
}
string Teahcer::getName()
{
return m_strName;
}
void Teacher::setAge(int age)
{
m_iAge = age;
}
int Teacher::getAge()
{
return m_iAge;
}
demo.cpp
#include<iostream>
#include<stdlib.h>
#include<string>
#include"Teacher.h"
void test(Teacher t)//参数传递的例子
{
}
int main(void)
{
Teacher t1;//实例化一个对象t1(栈中)
Teacher *P = new Teacher()//从堆中实例化一个对象
delete p;//结果中的~Teacher 是调用这行代码打印出来的
system("pause");
return 0;
}
结果:
改变demo.cpp:
#include<iostream>
#include<stdlib.h>
#include<string>
#include"Teacher.h"
void test(Teacher t)//参数传递的例子
{
}
int main(void)
{
Teacher t1;//实例化一个对象t1(栈中),调用普通构造函数
Teacher t2(t1);//调用拷贝构造函数
system("pause");
return 0;
}
按任意键继续可有两行析构函数
C++对象数组
Coordinate.h
class Coordinate
{
public:
Coordinate();
~Coordinate();
public:
int m_iX;
int m_iY;
};
Coordinate.cpp
#include<iostream>
#include"Coordinate.h"
using namespace std;
Coordinate::Coordinate()
{
cout << "Coordinate" << endl;
}
Coordinate::~Coordinate()
{
cout << "~Coordinate" << endl;
}
demo.cpp
#include<iostream>
#include<stdlib.h>
#include"Coordinate.h"
using namespace std;
int main(void)
{
Coordinate coor[3];//从栈中实例化一个数组
coor[0].m_iX = 3;
coor[0].m_iY = 5;
Coordinate *p = new Coordinate[3];//在堆中实例化一个数组
p->m_iX = 7;
p[0].m_iY = 9;
p++;//p=p+1或p+=1;指针指向下一个元素
p->m_iX = 11;
p[0].m_iY = 13;//实际访问的是数组的第二个元素,虽然是p[0]
p[1].m_iX = 15;
p++;
p->m_iY = 17;
for (int i = 0; i < 3; i++)
{
cout <<"coor.x"<< coor[i].m_iX << endl;
cout << "coor.y"<<coor[i].m_iY << endl;
}//遍历第一个数组
for (int j = 0; j < 3; j++)
{
cout <<"p_x" << p->m_iX << endl;
cout << "p_y" << p->m_iY << endl;//此时打印出来的是第三个元素
p--;
}//遍历第二个数组
p++;//让指针指回到第一个元素中来
delete[]p;//不能单独用这条语句释放内存,要加上p++
p = NULL;
system("pause");
return 0;
}
三次在栈中执行实例化,三次在堆中执行实例化
栈中的2,3个元素都是随机值,不可控;
堆中是倒叙打印出来的
最后执行三次析构函数(delete得来的)
对象数组实例2
Coordinate *p = new Coordinate[3];//在堆中实例化一个数组
这个数组中的每一个对象都执行了他的构造函数,
delete[]p;在销毁他的时候,我们也希望能够执行他的每一个析构函数来进行完美的销毁
若不加[],销毁的仅仅是第一个元素;只有一个~Coordinate,出现内存泄露
C++对象成员
Line.h
#include"Coordinate.h"
class Line
{
public:
Line();//默认的构造函数,没有参数
~Line();
void setA(int x, int y);
void setB(int x, int y);
void printInfo();
private:
Coordinate m_coorA;
Coordinate m_coorB;
};
Line.cpp
#include<iostream>
#include"Line.h"
using namespace std;
Line::Line()//默认的构造函数,没有参数
{
cout << "Line()" << endl;
}
Line::~Line()
{
cout << "~Line()" << endl;
}
void Line::setA(int x, int y)
{
m_coorA.setX(x);
m_coorA.setY(y);
}
void setB(int x, int y)
{
m_coorB.setX(x);
m_coorB.setY(y);
}
void printInfo() // 信息的打印
{
cout << "("<< m_coorA.getX() << ","<< m_coorA.getY() << ")"<< endl;
cout << "(" << m_coorB.getX() << "," << m_coorB.getY() << ")" << endl;
}
Coordinate.h
class Coordinate
{
public:
Coordinate();//构造函数
~Coordinate();//析构函数
void setX(int x);
int getX();
void setY(int y);
int getY();//数据封装函数
private:
int m_iX;
int m_iY;//数据成员
};
Cooridinate.cpp
#include<iostream>
#include"Coordinate.h"
using namespace std;
Coordinate::Coordinate()//调用构造函数
{
cout << "Coordinate()" << endl;
}
Coordinate::~Coordinate()//调用析构函数
{
cout << "~Coordinate()" << endl;
}
void Coordinate::setX(int x)
{
m_iX = x;
}
int Coordiante::getX()
{
return m_iX;
}//横坐标的一对数据封装函数
void Coordinate::setY(int y)
{
m_iY = y;
}
int Coordinate::getY()//数据封装函数
{
return m_iY;
}
demo.cpp
#include<iostream>
#include<stdlib.h>
#include"Line.h"
#include"Coordinate.h"
using namespace std;
int main(void)
{
Line *p = new Line();//定义一个p指针
delete p;
p = NULL;
system("pause");
return 0;
}
运行结果
ps:首先创建两个点A,B,然后创建直线;在销毁时正好相反,现销毁线段再销毁A,B两个坐标。
实例二:
注:**我们需要让线段的类是带有参数的,并且这个参数将来能够传递给这两个点—>让构建函数有参数Coordinate(int x,int y),Line(int x1,int x2,int y1,int y2)
Line.h
#include"Coordinate.h"
class Line
{
public:
Line(int x1,int x2, int y1, int y2);//默认的构造函数,没有参数
~Line();
void setA(int x, int y);
void setB(int x, int y);
void printInfo();
private:
Coordinate m_coorA;
Coordinate m_coorB;
};
line.cpp
#include<iostream>
#include"Line.h"
using namespace std;
Line::Line(int x1,int y1,int x2,int y2 ):m_coorA(x1,y1),m_coorB(x2,y2) //初始化列表来实现这两个对象成员
{
cout << "Line()" << endl;
}
Line::~Line()
{
cout << "~Line()" << endl;
}
void Line::setA(int x, int y)
{
m_coorA.setX(x);
m_coorA.setY(y);
}
void setB(int x, int y)
{
m_coorB.setX(x);
m_coorB.setY(y);
}
void printInfo() // 信息的打印
{
cout << "("<< m_coorA.getX() << ","<< m_coorA.getY() << ")"<< endl;
cout << "(" << m_coorB.getX() << "," << m_coorB.getY() << ")" << endl;
}
Coordinate.h
class Coordinate
{
public:
Coordinate(int x,int y);//构造函数
~Coordinate();//析构函数
void setX(int x);
int getX();
void setY(int y);
int getY();//数据封装函数
private:
int m_iX;
int m_iY;//数据成员
};
Coordinate.cpp
#include<iostream>
#include"Coordinate.h"
using namespace std;
Coordinate::Coordinate(int x,int y)//调用构造函数
{
m_iX = x;
m_iY = y;
cout << "Coordinate()" <<m_iX<<","<<m_iY<<endl;
}
Coordinate::~Coordinate()//调用析构函数
{
cout << "~Coordinate()" << m_iX << "," << m_iY<< endl;
}
void Coordinate::setX(int x)
{
m_iX = x;
}
int Coordiante::getX()
{
return m_iX;
}//横坐标的一对数据封装函数
void Coordinate::setY(int y)
{
m_iY = y;
}
int Coordinate::getY()//数据封装函数
{
return m_iY;
}
运行结果: