封装意义:
struct和class的区别:
成员属性私有化:
优点1,控制成员的读写:
设置name可读可写:
#include<iostream>
#include<String>
using namespace std;
class Student {
public://公共方法赋值
void setname(string name1)
{
name = name1;
}
string getname()
{
return name;
}
private://私有属性,只有本类可用
string name;
int age;
int passwad;
};
int main()
{
Student s1;
s1.setname("张");
cout << "name:" << s1.getname() << endl;
}
设置age只读:
#include<iostream>
#include<String>
using namespace std;
class Student {
public:
//设置姓名
void setname(string name1)
{
name = name1;
}
//获取姓名
string getname()
{
return name;
}
//获取年龄
int getage()
{
int age = 18;
return age;
}
private:
string name;//设置可读可写
int age;//只读
int passward;//只写
};
int main()
{
Student s1;
s1.setname("张");
cout << "name:" << s1.getname() << endl;
cout << "age:" << s1.getage();
}
设置password只写:
#include<iostream>
#include<String>
using namespace std;
class Student {
public:
//设置姓名
void setname(string name1)
{
name = name1;
}
//获取姓名
string getname()
{
return name;
}
//获取年龄
int getage()
{
int age = 18;
return age;
}
//设置密码 只写 外界获取不到
void setpassword(int password1)
{
password = password1;
}
private:
string name;//设置可读可写
int age;//只读
int password;//只写
};
int main()
{
Student s1;
s1.setname("张");
cout << "name:" << s1.getname() << endl;
cout << "age:" << s1.getage();
//只可以设置 外界不可以获取
s1.setpassword(123456);
//cout << "password:" << s1.setpassword() << endl;
}
优点2,可以检测数据的有效性:
#include<iostream>
#include<String>
using namespace std;
class Student {
public:
//设置姓名
void setage(int age1)
{
//检验年龄的有效性
if (age1 < 0 || age1>150)
{
age = 0;
cout << "你输入的年龄有误" << endl;
return;
}
age = age1;
}
//获取年龄
int getage()
{
return age;
}
private:
string name;//设置可读可写
int age;//只读
int password;//只写
};
int main()
{
Student s1;
s1.setage(180);
//s1.setage(18);
cout << "年龄为:" << s1.getage() << endl;
}
练习案例1:
#include<iostream>
#include<String>
using namespace std;
class cube {
public:
int caculateS(int c1, int w1, int h1)
{
return 2 * c1 * w1 + 2 * w1 * h1 + 2 * c1 * h1;
}
int caculateV(int c1, int w1, int h1)
{
return c1 * w1 * h1;
}
//行为:
//设置获取长宽高
void seth(int h1)
{
h = h1;
}
int geth()
{
return h;
}
void setw(int w1)
{
w = w1;
}
int getw()
{
return w;
}
void setc(int c1)
{
c = c1;
}
int getc()
{
return c;
}
bool isSameByClass(cube &c2)
{
if (c == c2.getc() && h == c2.geth() && w == c2.getw())
{
return true;
}
return false;
}
//属性:
private:
int h;
int w;
int c;
};
//全局函数判断
bool issame(cube &c1,cube &c2)
{
if (c1.getc() == c2.getc() &&c1.geth( )==c2.geth()&&c1.getw()==c2.getw())
{
return true;
}
return false;
}
int main()
{
cube c1;
c1.setc(10);
c1.setw(10);
c1.seth(10);
cout << "c1面积为:" << c1.caculateS(10,10,10) << endl;
cout << "c1体积为:" << c1.caculateV(10,10,10) << endl;
cube c2;
c2.setc(10);
c2.setw(10);
c2.seth(5);
//利用全局函数判断两个立方体是否相等
bool result=issame(c1, c2);
if (result)
{
cout << "c1,c2面积体积相同" << endl;
}
else {
cout << "c1,c2面积体积不相同" << endl;
}
//利用成员函数判断两个立方体是否相等
bool result1 = c1.isSameByClass(c2);
if (result1)
{
cout << "成员函数,c1,c2面积体积相同" << endl;
}
else
{
cout << "成员函数,c1,c2面积体积不相同" << endl;
}
}
练习案例2:
对象的初始化和清理:
构造函数和析构函数:
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
//1.构造函数
//没有返回值 不用写void
//函数名与类名相同
//构造函数可以有参数,可以发送重载
//创建对象的时候,构造函数会自动调用,而且只调用一次
person()
{
cout << "person 构造函数的调用" << endl;
}
};
void test01()
{
person p; // 创建对象的时候,构造函数会自动调用,而且只调用一次
}
int main()
{
test01();
}
当person p;在全局区时
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
//1.构造函数
//没有返回值 不用写void
// 函数名与类名相同
//构造函数可以有参数,可以发送重载
//创建对象的时候,构造函数会自动调用,而且只调用一次
person()
{
cout << "person 构造函数的调用" << endl;
}
//2.析构函数 进行清理的操作
// 没有返回值 不写void
// 函数名和类名相同 在名称前加~
// 析构函数不可以有参数的,不可以发生重载
// 对象在销毁前 会自动调用析构函数,而且只会调用一次
~person()
{
cout << "person 的析构函数" << endl;
}
};
void test01()
{
person p;
}
int main()
{
test01();
}
2.当person p;在main()中:会等35行执行后,按任意键等所有程序结束,p被销毁后,析构函数自动调用
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
//1.构造函数
//没有返回值 不用写void
// 函数名与类名相同
//构造函数可以有参数,可以发送重载
// 创建对象的时候,构造函数会自动调用,而且只调用一次
person()
{
cout << "person 构造函数的调用" << endl;
}
//2.析构函数 进行清理的操作
// 没有返回值 不写void
// 函数名和类名相同 在名称前加~
// 析构函数不可以有参数的,不可以发生重载
// 对象在销毁前 会自动调用析构函数,而且只会调用一次
~person()
{
cout << "person 析构函数的调用" << endl;
}
};
//构造和析构都是必须有的实现,如果我们自己不提供,编译器会提供一个空实现的构造和析构
void test01()
{
person p;
}
int main()
{
person p;
//test01();
system("pause");
return 0;
}
构造函数的分类及调用:
两种分类:
按照参数分类: 无参构造(默认构造)和有参构造
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
cout << "peroson的有参构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
};
int main()
{
}
按照类型分类: 普通构造函数和拷贝构造函数
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
}
int age;
};
int main()
{
}
调用:
1括号法:
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void test01()
{
//括号法
person p1;//无参构造函数
person p2(10);//有参构造函数
person p3(p2);//拷贝构造函数
cout << "p2的年龄" << p2.age << endl;
cout << "p3的年龄" << p3.age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
注意事项:
2.显示法:
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void test01()
{
//括号法
//person p1;//无参构造函数
//person p2(10);//有参构造函数
//person p3(p2);//拷贝构造函数
//cout << "p2的年龄" << p2.age << endl;
//cout << "p3的年龄" << p3.age << endl;
//显示法
person p1;
person p2 = person(10);//有参构造函数
person p3 = person(p2);//拷贝构造函数
//person(10);
//cout << "aaa" << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
注意事项2:匿名类
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void test01()
{
person(10);//匿名对象:特点 这行执行结束后,系统会立即回收掉匿名对象
cout << "aaa" << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3隐式转换法:
拷贝构造函数的调用时机:
使用一个已经创建完毕的对象来初始话一个新对象
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void test01()
{
//1. 使用一个已经创建完毕的对象来初始话一个新对象
person p1(20);
person p2(p1);
}
int main()
{
test01();
}
值传递的方式给函数参数传值
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void dowork(person p)
{
}
void test02()
{
//2.值传递的方式给函数参数传值
person p;
dowork(p);
}
int main()
{
test02();
}
值方式返回局部对象:理解的一半
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
person dowork()
{
person p1;
cout << (int*)&p1 << endl;
return p1;
}
void test03()
{
person p = dowork();
cout << (int*)&p << endl;
}
int main()
{
test03();
}
构造函数调用规则:
示例代码:
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
person()
{
cout << "peroson的无参构造函数" << endl;
}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void test01()
{
person p;
p.age = 18;
cout << "p的年龄为" << p.age << endl;
}
int main()
{
test01();
}
不对20-24行代码进行注释:
对20-24行代码进行注释:
说明:定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
示例代码;
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
//person()
//{
//cout << "peroson的无参构造函数" << endl;
//}
//有参构造函数
person(int a)
{
age = a;
cout << "peroson的有参构造函数" << endl;
}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void test01()
{
//person p;//调用无参构造
person p(29);//调用有参构造
person p2(p);//调用拷贝构造
cout << "p2的年龄为" << p2.age << endl;
}
}
int main()
{
test01();
}
注释掉8-11行代码后,在35行调用无参构造运行报错,说明定义有参构造函数后,c++不在提供默认无参构造
2.注释掉19-23行代码后,在37行调用拷贝构造函数时,运行结构如下图,说明用户定义有参构造后,(不会提供默认构造),但是会提供默认拷贝构造函数,相当于编译器自动写了第21行的代码。
示例代码:
#include<iostream>
#include<string>
using namespace std;
class person {
public:
//按照参数分类 无参构造(默认构造)和有参构造
//无参构造函数
//person()
//{
//cout << "peroson的无参构造函数" << endl;
//}
//有参构造函数
//person(int a)
//{
//age = a;
//cout << "peroson的有参构造函数" << endl;
//}
//拷贝构造函数
person(const person &p)
{
age = p.age;
cout << "peroson的拷贝构造函数" << endl;
}
//析构函数
~person()
{
cout << "person的析构函数" << endl;
}
int age;
};
void test01()
{
person p;
person p(29);
person p2(p);
cout << "p2的年龄为" << p2.age << endl;
}
int main()
{
test01();
}