C++远征之封装篇(笔记)
2018-01-16 14:43
190 查看
2-2、对象实例化练习代码
//(C++远征之封装篇2-2)对象实例化练习
#include<iostream>
#include<stdlib.h>
using namespace std;
class Coordinate
{
public:
int x;
int y;
void printX()
{
cout<<"x = "<<x<<endl;
}
void printY()
{
cout<<"y = "<<y<<endl;
}
};
int main(void)
{
//定义栈中的对象
Coordinate coor;
coor.x = 10;
coor.y = 20;
coor.printX();
coor.printY();
//定义堆中的对象
Coordinate *p = new Coordinate();
if(p == NULL)
{
//内存申请失败
return 0;
}
p->x = 100;
p->y = 200;
p->printX();
p->printY();
delete p;
//注意释放内存
p = NULL;
system("pause");
return 0;
}
3-1C++初始String
代码演示:
//(C++远征之封装篇3-1C++初始String)
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/*************************************/
/*题目描述:
1.提示用户输入姓名
2.接收用户的输入
3.然后向用户问好,HELLO XXX
4.告诉用户名字的长度
5.告诉用户名字的首字母是什么
6.如果用户直接输入回车,那么告诉用户的输入为空
7.如果用户输入的是imooc,那么告诉用户的角色是管理员
/*************************************/
int main(void)
{
string name;
cout<<"Please input your name:";
getline(cin, name);
if(name.empty())
{
cout<<"input is null..."<<endl;
system("pause");
return 0;
}
if(name == "imooc")
{
cout<<"you are a administrator"<<endl;
}
cout<<"hello "+name<<endl;
cout<<"your name lenth:"<<name.size()<<endl;
cout<<"the initial of your name is :"<<name[0]<<endl;
system("pause");
return 0;
}
4-2、C++属性封装
代码演示:
//4-2C++属性封装代码演示
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/****************************/
/* 数据的封装
定义一个Student类,包含以下信息:
1.姓名:name
2.性别:gender
3.学分(只读):score
4.学习:study */
/****************************/
class Student
{
public:
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setGender(string _gender)
{
m_strGender = _gender;
}
string getGender()
{
return m_strGender;
}
int getScore()
{
return m_iScore;
}
void initScore()
{
m_iScore = 0;
}
void study(int _score)
{
m_iScore += _score;//等价于m_iScore = m_iScore + _score;
}
private:
string m_strName;
string m_strGender;
int m_iScore;
};
int main(void)
{
Student stu;
stu.initScore();
//给score赋初始值
stu.setName("lisi");
stu.setGender("男");
stu.study(5);
stu.study(3);
cout<<stu.getName()<<" "<<stu.getGender()<<" "<<stu.getScore()<<endl;
system("pause");
return 0;
}
5-2、类外定义
1.同文件类外定义
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/********************************/
/*定义一个Teacher类,要求分别采用同文件类外定义和分文件类外定义
的方式完成,具体要求如下:
数据成员:
1.名字
2.年龄
3.性别
成员函数:
1.数据成员的的封装函数
2.授课teach
/********************************/
class Teacher
{
public:
void setName(string _name);
string getName();
void setGender(string _gender);
string getGender();
void setAge(int _age);
int getAge();
void teach();
private:
string m_strName;
string m_strGender;
int m_iAge;
};
void Teacher::setName(string _name)
{
m_strName = _name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setGender(string _gender)
{
m_strGender = _gender;
}
string Teacher::getGender()
{
return m_strGender;
}
void Teacher::setAge(int _age)
{
m_iAge = _age;
}
int Teacher::getAge()
{
return m_iAge;
}
void Teacher::teach()
{
cout<<"现在上课......"<<endl;
}
int main(void)
{
Teacher t;
t.setName("孔子");
t.setGender("男");
t.setAge(45);
cout<<t.getName()<<" "<<t.getGender()<<" "<<t.getAge()<<endl;
t.teach();
system("pause");
return 0;
}
2.分文件类外定义
Teacher.h文件
#include<String>
using namespace std;
class Teacher
{
public:
void setName(string _name);
string getName();
void setGender(string _gender);
string getGender();
void setAge(int _age);
int getAge();
void teach();
private:
string m_strName;
string m_strGender;
int m_iAge;
};
Teacher.cpp文件
#include<iostream>
#include<String>
#include"Teacher.h"
using namespace std;
void Teacher::setName(string _name)
{
m_strName = _name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setGender(string _gender)
{
m_strGender = _gender;
}
string Teacher::getGender()
{
return m_strGender;
}
void Teacher::setAge(int _age)
{
m_iAge = _age;
}
int Teacher::getAge()
{
return m_iAge;
}
void Teacher::teach()
{
cout<<"现在上课......"<<endl;
}
Teacher_demo.cpp文件
#include<iostream>
#include<String>
#include"Teacher.h"
using namespace std;
int main(void)
{
Teacher t;
t.setName("孔子");
t.setGender("男");
t.setAge(45);
cout<<t.getName()<<" "<<t.getGender()<<" "<<t.getAge()<<endl;
t.teach();
system("pause");
return 0;
}
6-2构造函数练习
代码:
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/****************************************/
/*Teacher类
自定义无参构造函数
自定义有参构造函数
数据:
名字
年龄
成员函数:
数据成员的封装函数*/
/****************************************/
class Teacher
{
public:
//无参构造函数
Teacher()
{
m_strName = "Jim";
m_iAge = 20;
cout<<"Teacher()"<<endl;
}
//有参构造函数
Teacher(string name,int age=10)
{
m_strName = name;
m_iAge = age;
cout<<"Teacher(string name,int age)"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
return m_iAge;
}
private:
string m_strName;
int m_iAge;
};
int main(void)
{
Teacher t1;
Teacher t2("Merry",18);
Teacher t3("James");
cout<<t1.getName()<<" "<<t1.getAge()<<endl;
cout<<t2.getName()<<" "<<t2.getAge()<<endl;
cout<<t3.getName()<<" "<<t3.getAge()<<endl;
system("pause");
return 0;
}
6-5、初始化列表
代码:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
/**************************************/
/*Teacher类
自定义有参默认构造函数
使用初始化列表初始化数据
数据:
名字
年龄
成员函数:
数据成员的封装函数
拓展:
定义可以带最多学生的个数,此为常量
/**************************************/
class Teacher
{
public:
//有参构造函数
/* //普通初始化方法
Teacher(string name = "Jim",int age=1)
{
m_strName = name;
m_iAge = age;
cout<<"Teacher(string name,int age)"<<endl;
}
*/
//使用初始化列表初始化
Teacher(string name = "Jim",int age=1,int max=100):m_strName(name),m_iAge(age),m_iMax(max){
cout<<"Teacher(string name,int age)"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
return m_iAge;
}
int getMax()
{
return m_iMax;
}
private:
string m_strName;
int m_iAge;
const int m_iMax;
};
int main(void)
{
Teacher t1("Merry",15,150);
cout<<t1.getName()<<" "<<t1.getAge()<<" "<<t1.getMax()<<endl;
system("pause");
return 0;
}
6-8、拷贝构造函数
代码演示:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
/**************************************/
/*Teacher类
自定义拷贝构造函数
数据:
名字
年龄
成员函数:
数据成员的封装函数
/**************************************/
class Teacher
{
public:
//有参构造函数
//使用初始化列表初始化
Teacher(string name = "Jim",int age=1):m_strName(name),m_iAge(age){
cout<<"Teacher(string name,int age)"<<endl;
}
//拷贝构造函数
Teacher(const Teacher& tea)
{
cout<<"Teacher(const Teacher& tea)"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
return m_iAge;
}
private:
string m_strName;
int m_iAge;
};
void test(Teacher t)
{
}
int main(void)
{
Teacher t1;
// Teacher t2 = t1;
// Teacher t3(t1);
test(t1);
//调用函数的方法触发拷贝构造函数
system("pause");
return 0;
}
6-11、析构函数
代码:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
/**************************************/
/*Teacher类
1.自定义析构函数
2.普通方式实例化对象,在销毁对象时是否自动调用析构函数
3.通过拷贝构造函数实例化对象,在销毁对象时是否自动调用析构函数
数据:
名字
年龄
成员函数:
数据成员的封装函数
/**************************************/
class Teacher
{
public:
//有参构造函数
//使用初始化列表初始化
Teacher(string name = "Jim",int age=1):m_strName(name),m_iAge(age){
cout<<"Teacher(string name,int age)"<<endl;
}
//拷贝构造函数
Teacher(const Teacher& tea)
{
cout<<"Teacher(const Teacher& tea)"<<endl;
}
//析构函数
~Teacher()
{
cout<<"~Teacher()"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
943a
return m_iAge;
}
private:
string m_strName;
int m_iAge;
};
void test(Teacher t){ }
int main(void)
{
Teacher t1;
//通过栈实例化对象
Teacher t2(t1);
// Teacher *p = new Teacher();
//通过堆实例化对象
// delete p;
// test(t1);
//调用函数的方法触发拷贝构造函数
system("pause");
return 0;
}
//(C++远征之封装篇2-2)对象实例化练习
#include<iostream>
#include<stdlib.h>
using namespace std;
class Coordinate
{
public:
int x;
int y;
void printX()
{
cout<<"x = "<<x<<endl;
}
void printY()
{
cout<<"y = "<<y<<endl;
}
};
int main(void)
{
//定义栈中的对象
Coordinate coor;
coor.x = 10;
coor.y = 20;
coor.printX();
coor.printY();
//定义堆中的对象
Coordinate *p = new Coordinate();
if(p == NULL)
{
//内存申请失败
return 0;
}
p->x = 100;
p->y = 200;
p->printX();
p->printY();
delete p;
//注意释放内存
p = NULL;
system("pause");
return 0;
}
3-1C++初始String
代码演示:
//(C++远征之封装篇3-1C++初始String)
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/*************************************/
/*题目描述:
1.提示用户输入姓名
2.接收用户的输入
3.然后向用户问好,HELLO XXX
4.告诉用户名字的长度
5.告诉用户名字的首字母是什么
6.如果用户直接输入回车,那么告诉用户的输入为空
7.如果用户输入的是imooc,那么告诉用户的角色是管理员
/*************************************/
int main(void)
{
string name;
cout<<"Please input your name:";
getline(cin, name);
if(name.empty())
{
cout<<"input is null..."<<endl;
system("pause");
return 0;
}
if(name == "imooc")
{
cout<<"you are a administrator"<<endl;
}
cout<<"hello "+name<<endl;
cout<<"your name lenth:"<<name.size()<<endl;
cout<<"the initial of your name is :"<<name[0]<<endl;
system("pause");
return 0;
}
4-2、C++属性封装
代码演示:
//4-2C++属性封装代码演示
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/****************************/
/* 数据的封装
定义一个Student类,包含以下信息:
1.姓名:name
2.性别:gender
3.学分(只读):score
4.学习:study */
/****************************/
class Student
{
public:
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setGender(string _gender)
{
m_strGender = _gender;
}
string getGender()
{
return m_strGender;
}
int getScore()
{
return m_iScore;
}
void initScore()
{
m_iScore = 0;
}
void study(int _score)
{
m_iScore += _score;//等价于m_iScore = m_iScore + _score;
}
private:
string m_strName;
string m_strGender;
int m_iScore;
};
int main(void)
{
Student stu;
stu.initScore();
//给score赋初始值
stu.setName("lisi");
stu.setGender("男");
stu.study(5);
stu.study(3);
cout<<stu.getName()<<" "<<stu.getGender()<<" "<<stu.getScore()<<endl;
system("pause");
return 0;
}
5-2、类外定义
1.同文件类外定义
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/********************************/
/*定义一个Teacher类,要求分别采用同文件类外定义和分文件类外定义
的方式完成,具体要求如下:
数据成员:
1.名字
2.年龄
3.性别
成员函数:
1.数据成员的的封装函数
2.授课teach
/********************************/
class Teacher
{
public:
void setName(string _name);
string getName();
void setGender(string _gender);
string getGender();
void setAge(int _age);
int getAge();
void teach();
private:
string m_strName;
string m_strGender;
int m_iAge;
};
void Teacher::setName(string _name)
{
m_strName = _name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setGender(string _gender)
{
m_strGender = _gender;
}
string Teacher::getGender()
{
return m_strGender;
}
void Teacher::setAge(int _age)
{
m_iAge = _age;
}
int Teacher::getAge()
{
return m_iAge;
}
void Teacher::teach()
{
cout<<"现在上课......"<<endl;
}
int main(void)
{
Teacher t;
t.setName("孔子");
t.setGender("男");
t.setAge(45);
cout<<t.getName()<<" "<<t.getGender()<<" "<<t.getAge()<<endl;
t.teach();
system("pause");
return 0;
}
2.分文件类外定义
Teacher.h文件
#include<String>
using namespace std;
class Teacher
{
public:
void setName(string _name);
string getName();
void setGender(string _gender);
string getGender();
void setAge(int _age);
int getAge();
void teach();
private:
string m_strName;
string m_strGender;
int m_iAge;
};
Teacher.cpp文件
#include<iostream>
#include<String>
#include"Teacher.h"
using namespace std;
void Teacher::setName(string _name)
{
m_strName = _name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setGender(string _gender)
{
m_strGender = _gender;
}
string Teacher::getGender()
{
return m_strGender;
}
void Teacher::setAge(int _age)
{
m_iAge = _age;
}
int Teacher::getAge()
{
return m_iAge;
}
void Teacher::teach()
{
cout<<"现在上课......"<<endl;
}
Teacher_demo.cpp文件
#include<iostream>
#include<String>
#include"Teacher.h"
using namespace std;
int main(void)
{
Teacher t;
t.setName("孔子");
t.setGender("男");
t.setAge(45);
cout<<t.getName()<<" "<<t.getGender()<<" "<<t.getAge()<<endl;
t.teach();
system("pause");
return 0;
}
6-2构造函数练习
代码:
#include<iostream>
#include<stdlib.h>
#include<String>
using namespace std;
/****************************************/
/*Teacher类
自定义无参构造函数
自定义有参构造函数
数据:
名字
年龄
成员函数:
数据成员的封装函数*/
/****************************************/
class Teacher
{
public:
//无参构造函数
Teacher()
{
m_strName = "Jim";
m_iAge = 20;
cout<<"Teacher()"<<endl;
}
//有参构造函数
Teacher(string name,int age=10)
{
m_strName = name;
m_iAge = age;
cout<<"Teacher(string name,int age)"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
return m_iAge;
}
private:
string m_strName;
int m_iAge;
};
int main(void)
{
Teacher t1;
Teacher t2("Merry",18);
Teacher t3("James");
cout<<t1.getName()<<" "<<t1.getAge()<<endl;
cout<<t2.getName()<<" "<<t2.getAge()<<endl;
cout<<t3.getName()<<" "<<t3.getAge()<<endl;
system("pause");
return 0;
}
6-5、初始化列表
代码:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
/**************************************/
/*Teacher类
自定义有参默认构造函数
使用初始化列表初始化数据
数据:
名字
年龄
成员函数:
数据成员的封装函数
拓展:
定义可以带最多学生的个数,此为常量
/**************************************/
class Teacher
{
public:
//有参构造函数
/* //普通初始化方法
Teacher(string name = "Jim",int age=1)
{
m_strName = name;
m_iAge = age;
cout<<"Teacher(string name,int age)"<<endl;
}
*/
//使用初始化列表初始化
Teacher(string name = "Jim",int age=1,int max=100):m_strName(name),m_iAge(age),m_iMax(max){
cout<<"Teacher(string name,int age)"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
return m_iAge;
}
int getMax()
{
return m_iMax;
}
private:
string m_strName;
int m_iAge;
const int m_iMax;
};
int main(void)
{
Teacher t1("Merry",15,150);
cout<<t1.getName()<<" "<<t1.getAge()<<" "<<t1.getMax()<<endl;
system("pause");
return 0;
}
6-8、拷贝构造函数
代码演示:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
/**************************************/
/*Teacher类
自定义拷贝构造函数
数据:
名字
年龄
成员函数:
数据成员的封装函数
/**************************************/
class Teacher
{
public:
//有参构造函数
//使用初始化列表初始化
Teacher(string name = "Jim",int age=1):m_strName(name),m_iAge(age){
cout<<"Teacher(string name,int age)"<<endl;
}
//拷贝构造函数
Teacher(const Teacher& tea)
{
cout<<"Teacher(const Teacher& tea)"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
return m_iAge;
}
private:
string m_strName;
int m_iAge;
};
void test(Teacher t)
{
}
int main(void)
{
Teacher t1;
// Teacher t2 = t1;
// Teacher t3(t1);
test(t1);
//调用函数的方法触发拷贝构造函数
system("pause");
return 0;
}
6-11、析构函数
代码:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
/**************************************/
/*Teacher类
1.自定义析构函数
2.普通方式实例化对象,在销毁对象时是否自动调用析构函数
3.通过拷贝构造函数实例化对象,在销毁对象时是否自动调用析构函数
数据:
名字
年龄
成员函数:
数据成员的封装函数
/**************************************/
class Teacher
{
public:
//有参构造函数
//使用初始化列表初始化
Teacher(string name = "Jim",int age=1):m_strName(name),m_iAge(age){
cout<<"Teacher(string name,int age)"<<endl;
}
//拷贝构造函数
Teacher(const Teacher& tea)
{
cout<<"Teacher(const Teacher& tea)"<<endl;
}
//析构函数
~Teacher()
{
cout<<"~Teacher()"<<endl;
}
//封装函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setAge(int _age)
{
m_iAge = _age;
}
int getAge()
{
943a
return m_iAge;
}
private:
string m_strName;
int m_iAge;
};
void test(Teacher t){ }
int main(void)
{
Teacher t1;
//通过栈实例化对象
Teacher t2(t1);
// Teacher *p = new Teacher();
//通过堆实例化对象
// delete p;
// test(t1);
//调用函数的方法触发拷贝构造函数
system("pause");
return 0;
}
相关文章推荐
- C++基础学习笔记----第八课(类的封装)
- 【笔记】(C++远征之离港篇)C++函数新特征
- c++远征之封装篇(拷贝构造函数)
- 【笔记】(C++远征之离港篇)const
- C++远征之继承篇(笔记)
- 【笔记】(C++远征之离港篇)内存管理
- c++远征之封装篇(对象)
- c++远征之封装篇(对象指针)
- 慕课学习C++笔记02-封装(下)
- C++思考笔记-----Ruminations on C++ 之 被封装的指针----句柄
- C++远征之封装篇(下)
- 学习笔记之深入浅出MFC 第8章 C++重要性质-----类封装
- STL源码笔记(17)—二叉排序树BST(C++封装)
- STL源码笔记(18)—平衡二叉树AVL(C++封装+模板)
- C++远征之多态篇(笔记)
- c++学习笔记之封装篇(上)
- onvif学习笔记7:一个C++封装的onvif代码的阅读笔记
- c++学习笔记之封装篇(上)
- VC++编程之第二课笔记――C++的继承封装多态
- C++远征之封装篇下