C++远征之封装篇(笔记)

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()
{
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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值