C++小白慕课网的一些程序2
来源:互联网 发布:linux关机 编辑:程序博客网 时间:2024/06/01 10:42
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;}
运行结果:
阅读全文
0 0
- 一些简单的C程序
- C语言的一些程序
- C的一些简单程序
- 积累的c/c++ 一些小程序
- C语言的一些测试程序
- 一些经典的C语言程序算法
- c语言编写的一些小程序
- [C++]C++的一些小程序
- c语言写的一些小程序
- c语言写的一些小程序
- 一些简单的java,c程序
- C++小白慕课网的一些程序2
- 一些c小程序
- 用C语言思想改写的用筛法求质数程序(第2修订版) 的一些源代码
- 关于C程序运行时的内存 的一些理解
- C语言中会常用到的一些小程序
- Linux C 文件传输程序(TCP && UDP) 遇到的一些问题
- 关于在R中使用C程序的一些问题
- mochajs测试框架的使用
- 内部类----局部内部类
- 类方法的直接调用
- (四)JUnit测试套件使用及参数化设置
- ThinkPHP3.2.3整合发送手机短信验证码
- C++小白慕课网的一些程序2
- 15个顶级Java多线程面试题及答案
- SQL--constraint 约束
- Emgu.CV学习(一) 介绍与安装
- Spring Boot (一):初识之入门篇
- spring-mvc框架配置详解
- [模拟]LeetCode 11. Container With Most Water 题解
- redis(2)——redis的数据结构
- BGP笔记-3