1.从c中结构体演化来的
2.定义一个类
//涉及到访问控制权限,后面再说
class 类名{
public:
公有的成员变量(数据/属性) 和 成员函数(方法/操作/行为)
private:
私有的成员变量(数据/属性) 和 成员函数(方法/操作/行为)
protected:
受保护的成员变量(数据/属性) 和 成员函数(方法/操作/行为)
};
例:
#include <iostream>
using namespace std;
class Circle{
public:
int r;
double area;
double get_area(){
area = 3.14 * r *r;
return area;
}
};
int main(int argc, const char *argv[])
{
Circle c1;
c1.r = 10;
double ret = c1.get_area();
cout<<ret<<endl;
return 0;
}
3.封装
面向对象的三大特征 : 封装 继承 多态,如果有四:加一个 抽象
3.1是面向对象的三大特征之一,将描述一类事物的变量(属性)和函数(方法)封装成一个整体
3.2需要用到访问控制权限
![](https://i-blog.csdnimg.cn/blog_migrate/c6c253bd142b9dccb220051aed9fd350.png)
访问控制权限有三种: public private protected
类内 子类中 类外
public ! ! !
protected ! ! X
private ! X X
3.3类中的成员函数可以访问自己类的成员变量(包括私有的成员变量)
3.4使用访问控制的好处
#include <iostream>
using namespace std;
class Color{
private:
int r;
int g;
int b;
public:
int get_color(){
return r+g+b;
}
void set_r(int red){
//自己写的成员函数给成员变量赋值
//逻辑可以自己写
//可以排查异常情况
if(0 <= red && red <= 255){
r = red;
}else{
//报错
}
}
};
int main(int argc, const char *argv[])
{
Color c1;
//有些时候在类外直接访问某些成员 可能会出现问题
//c1.r = 100000;//错误的 私有成员 不能再类外访问
c1.set_r(128);
return 0;
}
3.5访问控制权限的控制范围:
一旦出现了访问控制,后面所有的成员都受他的控制
直到出现了新的访问控制,或者类的定义结束
//允许出现很多个访问控制,但是一般同一类的都会归并到同一个访问控制下
练习:
1.优化上面圆的类,并增加能获取周长的函数。
#include <iostream>
using namespace std;
class Circle{
private:
int r;
double area;
double zhouchang;
public:
void set_r(int in_r){
r = in_r;
}
double get_area(){
area = 3.14 * r *r;
return area;
}
double get_zc(){
zhouchang = 2 * 3.14 * r;
return zhouchang;
}
};
int main(int argc, const char *argv[])
{
Circle c1;
c1.set_r(10);
double ret = c1.get_area();
cout<<ret<<endl;
double ret2 = c1.get_zc();
cout<<ret2<<endl;
return 0;
}
2.设计一个长方体的类(Cuboid)
包含:3个成员变量 -->长 宽 高
2个成员函数 -->设置长宽高 获取体积
实例化一个类对象(定义一个类变量)
输出该长方体对象的体积
#include <iostream>
using namespace std;
class Cuboid{
private:
int length;
int width;
int high;
public:
void init_cuboid(int l, int w, int h){
length = l;
width = w;
high = h;
}
int get_volume(){
return length * width * high;
}
};
int main(int argc, const char *argv[])
{
Cuboid c1;
c1.init_cuboid(3,4,5);
cout<<c1.get_volume()<<endl;
return 0;
}
3.设计一个全局函数,判断两个长方体是否相等
//长==长 宽==宽 高==高
#include <iostream>
using namespace std;
class Cuboid{
private:
int length;
int width;
int high;
public:
void init_cuboid(int l, int w, int h){
length = l;
width = w;
high = h;
}
int get_volume(){
return length * width * high;
}
int get_length(){
return length;
}
int get_width(){
return width;
}
int get_high(){
return high;
}
};
void judge(Cuboid &x, Cuboid &y){
if(x.get_length() == y.get_length() &&
x.get_width() == y.get_width() &&
x.get_high() == y.get_high()){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
int main(int argc, const char *argv[])
{
Cuboid c1;
c1.init_cuboid(3,4,5);
Cuboid c2;
c2.init_cuboid(3,4,6);
//cout<<c1.get_volume()<<endl;
judge(c1,c2);
return 0;
}
4.将判断长方体是否相等的全局函数,改成成员函数:
#include <iostream>
using namespace std;
class Cuboid{
private:
int length;
int width;
int high;
public:
void init_cuboid(int l, int w, int h){
length = l;
width = w;
high = h;
}
int get_volume(){
return length * width * high;
}
int get_length(){
return length;
}
int get_width(){
return width;
}
int get_high(){
return high;
}
//成员函数版
void judge(Cuboid &x, Cuboid &y){
if(x.get_length() == y.get_length() &&
x.get_width() == y.get_width() &&
x.get_high() == y.get_high()){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
//成员函数的重载版本 ---改进版
void judge(Cuboid &y){
//访问控制是针对整个类而言的,不是针对某个类对象
//下面的写法是对的
//成员函数可以访问私有成员变量
//即使是参数传递过来的 本类 对象,也是可以访问其私有成员的
if(length == y.length && width == y.width && high == y.high){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
};
//全局函数版
void judge(Cuboid &x, Cuboid &y){
if(x.get_length() == y.get_length() &&
x.get_width() == y.get_width() &&
x.get_high() == y.get_high()){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
int main(int argc, const char *argv[])
{
Cuboid c1;
c1.init_cuboid(3,4,5);
Cuboid c2;
c2.init_cuboid(3,4,5);
//cout<<c1.get_volume()<<endl;
c1.judge(c1,c2);
c1.judge(c2);
return 0;
}
4.类中成员函数的声明和定义分开写
#include <iostream>
using namespace std;
class Cuboid{
private:
int length;
int width;
int high;
public:
void init_cuboid(int, int, int);
int get_volume();
int get_length();
int get_width();
int get_high();
};
int Cuboid::get_high(){
return high;
}
int Cuboid::get_width(){
return width;
}
int Cuboid::get_length(){
return length;
}
int Cuboid::get_volume(){
return length * width * high;
}
void judge(Cuboid &x, Cuboid &y){
if(x.get_length() == y.get_length() &&
x.get_width() == y.get_width() &&
x.get_high() == y.get_high()){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
void Cuboid::init_cuboid(int l, int w, int h){
length = l;
width = w;
high = h;
}
int main(int argc, const char *argv[])
{
Cuboid c1;
c1.init_cuboid(3,4,5);
Cuboid c2;
c2.init_cuboid(3,4,6);
//cout<<c1.get_volume()<<endl;
judge(c1,c2);
return 0;
}