文章目录
类和对象
基本概念
- 类、对象、成员变量、成员函数
- 类的基本特性:
封装、继承、多态
类的封装
1. 把属性和方法进行封装
2. 对属性和方法进行控制
private
public
protected
3.没有访问控制限定的成员变量 函数 默认是私有属性
4. 类的内部和类的外部
public修饰成员变量和成员函数可以在类的内部和类的外部被访问
private修饰成员变量和成员函数只能在类的内部被访问
#include<iostream>
using namespace std;
class myCircle
{
public:
double getR()
{
return m_r;
}
void setR(double r)
{
m_r = r;
//return m_r;
}
double getS()
{
m_s = 3.14 * m_r * m_r;
return m_s;
}
private:
double m_r;
double m_s;
};
//类和指针
//类做函数参数
//类封装了成员函数和成员变量
void printMyCircle01(myCircle* mpc)
{
cout << "r:" << mpc->getR()<< endl;
cout << "s:" << mpc->getS()<< endl;
}
//类和引用
void printMyCircle02(myCircle& mpc)
{
cout << "r:" << mpc.getR() << endl;
cout << "s:" << mpc.getS() << endl;
}
int main()
{
myCircle c1, c2;
//c1.m_r = 10;//私有成员外部不能直接访问
c1.setR(10);
cout << c1.getS() << endl;
cout << endl;
c1.setR(20);
printMyCircle01(&c1);
cout << endl;
c1.setR(20);
printMyCircle02(c1);
return 0;
}
5. class 和 struct关键字的区别
在用struct定义类时,所有成员的默认属性为public
在用class定义类时,所有成员的默认属性为private
6. 类的声明和类的实现分开
类的声明
MyTeacher.h
#pragma once//只包含一次 防止重定义
//类的声明
class MyTeacher
{
public:
void setAge(int age);
int getAge();
private:
int m_age;
};
类的实现
MyTeacher.cpp
#include "MyTeacher.h"
//类的实现
//类域作用符 也相当于写在了类的内部
void MyTeacher::setAge(int age)
{
m_age = age;
}
int MyTeacher::getAge()
{
return m_age;
}
MyMain.cpp
#include<iostream>
#include "MyTeacher.h"
using namespace std;
int main()
{
MyTeacher t;
t.setAge(30);
cout << t.getAge() << endl;
return 0;
}
案例
1
设计立方体类(cube),求出立方体的面积和体积
求两个立方体,是否相等(全局函数和成员函数)
#include<iostream>
using namespace std;
//设计立方体类(cube),求出立方体的面积和体积
//比较立方体是否一样
class Cube
{
private:
int m_a;
int m_b;
int m_c;
int m_v;
int m_s;
public:
void setABC(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
int getA()
{
return m_a ;
}
int getB()
{
return m_b ;
}
int getC()
{
return m_c ;
}
int getS()
{
m_s = 2 * (m_a * m_b + m_a * m_c + m_b * m_c);
return m_s;
}
int getV()
{
m_v = m_a * m_b * m_c;
return m_v;
}
//②
//问题抛出 冗余
int judge(Cube& c1, Cube& c2)
{
if ((c1.getA() == c2.getA()) &&
(c1.getB() == c2.getB()) &&
(c1.getC() == c2.getC()))
{
return 1;
}
else
{
return 0;
}
}
//③
int judge(Cube& c2)
{
//这里的m_a是调用它的对象的成员变量
if( (m_a == c2.getA())&&
(m_b == c2.getB())&&
(m_c == c2.getC()))
{
return 1;
}
else
{
return 0;
}
}
};
//①全局函数
//1:same ; 0 : not same
int judge(Cube& c1, Cube& c2)
{
if ((c1.getA() == c2.getA())&&
(c1.getB() == c2.getB())&&
(c1.getC() == c2.getC()))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
Cube c1, c2;
c1.setABC(1, 2, 3);
c2.setABC(1, 2, 4);
cout << c1.getS() << endl;
cout << c1.getV() << endl;
//②
//冗余
if (c1.judge(c1, c2) == 1)
{
cout << "same" << endl;
}
else
{
cout << "not same" << endl;
}
//③
if (c1.judge(c2) == 1)//通过c1对象的类 调用成员函数
{
cout << "same" << endl;
}
else
{
cout << "not same" << endl;
}
//①
if (judge(c1,c2) == 1)
{
cout << "调用全局变量 same" << endl;
}
else
{
cout << "调用全局变量 not same" << endl;
}
return 0;
}
2
设计一个圆形类(Circle),和一个点类(Point),计算点在圆内部还是圆外
即:求点和圆的关系(圆内和圆外)
#include<iostream>
using namespace std;
class Point
{
private:
int p_x;
int p_y;
public:
void setPoint(int x, int y)
{
p_x = x;
p_y = y;
}
int getp_X()
{
return p_x;
}
int getp_Y()
{
return p_y;
}
};
class Circle
{
private:
int c_r;
int c_x;
int c_y;
public:
void setCircle(int r, int x, int y)
{
c_x = x;
c_y = y;
c_r = r;
}
int getR()
{
return c_r;
}
int getc_X()
{
return c_x;
}
int getc_Y()
{
return c_y;
}
int judge(Point& p)
{
//计算两点之间的距离
double a = abs((c_x - p.getp_X()) * (c_x - p.getp_X()) + (c_y - p.getp_Y()) * (c_y - p.getp_Y()));
//1:点在圆内 0:点在圆外
if (c_r >= a)
{
return 1;
}
else
{
return 0;
}
}
};
int main()
{
Circle c;
c.setCircle(10, 12,10);
Point p;
p.setPoint(10, 100);
//1 面向对象
if ((c.judge(p)) == 1)
{
cout << "点在圆内" << endl;
}
else
{
cout << "点在圆外" << endl;
}
//2
double a = abs((c.getc_X() - p.getp_X()) * (c.getc_X() - p.getp_X()) + (c.getc_Y() - p.getp_Y()) * (c.getc_Y() - p.getp_Y()));
if(c.getR() > a)
{
cout <<"点在圆内" << endl;
}
else
{
cout <<"点在圆外" << endl;
}
return 0;
}
3
把2中类的声明和实现分开
Circle.h
#pragma once
#include "Point.h"
class Circle
{
private:
int c_r;
int c_x;
int c_y;
public:
void setCircle(int r, int x, int y);
int getR();
int getc_X();
int getc_Y();
int judge(Point& p);
};
Circle.cpp
#include "Circle.h"
#include "Point.h"
#include<iostream>
void Circle::setCircle(int r, int x, int y)
{
c_x = x;
c_y = y;
c_r = r;
}
int Circle::getR()
{
return c_r;
}
int Circle::getc_X()
{
return c_x;
}
int Circle::getc_Y()
{
return c_y;
}
int Circle::judge(Point& p)
{
//计算两点之间的距离
double a = abs((c_x - p.getp_X()) * (c_x - p.getp_X()) + (c_y - p.getp_Y()) * (c_y - p.getp_Y()));
//1:点在圆内 0:点在圆外
if (c_r >= a)
{
return 1;
}
else
{
return 0;
}
}
Point.h
#pragma once
class Point
{
private:
int p_x;
int p_y;
public:
void setPoint(int x, int y);
int getp_X();
int getp_Y();
};
Point.cpp
#include "Point.h"
void Point::setPoint(int x, int y)
{
p_x = x;
p_y = y;
}
int Point::getp_X()
{
return p_x;
}
int Point::getp_Y()
{
return p_y;
}
main.cpp
#include<iostream>
#include "Circle.h"
#include "Point.h"
using namespace std;
int main()
{
Circle c;
c.setCircle(10, 12, 10);
Point p;
p.setPoint(10, 100);
//1
if ((c.judge(p)) == 1)
{
cout << "点在圆内" << endl;
}
else
{
cout << "点在圆外" << endl;
}
return 0;
}