//C++三大特性:封装、继承和多态
//封装的意义:将属性(成员变量)和行为(成员函数或者方法)作为一个整体,表现生活中的事物,将属性和行为加以权限控制
//设计一个圆类,求圆的周长
#include<iostream>
using namespace std;
#define PI 3.14
class Circle
{
//访问权限
public://公共权限
int m_R;
//行为
//获取圆的周长
double calculateZC()
{
return 2 * PI * m_R;
}
};
class Student
{
public:
string m_Name;
int m_ID;
void setName(string name)
{
m_Name = name;
}
void setID(int id)
{
m_ID = id;
}
void showStudent()
{
cout << "姓名:" << m_Name << endl << "学号:" << m_ID << endl;
}
};
int main()
{
Circle c1;
c1.m_R = 10;
cout << "圆的周长:" << c1.calculateZC() << endl;
Student s1;
s1.m_Name = "仙思凡";
s1.m_ID = 1001;
s1.showStudent();
Student s2;
s2.setID(2001);
s2.setName("aha");
s2.showStudent();
system("pause");
return 0;
}
网友评论