类
#include <iostream> using namespace std; // 声明一个盒子类 class Box { public: double length; // 长 double breadth; // 宽 double height; // 高 }; int main() { // 声明 Box 类对象 Box box1; Box box2; // 声明体积 double volumn = 0.0; // 实例化 box1 box1.length = 5.0; box1.breadth = 6.0; box1.height = 7.0; // 实例化 box2 box2.length = 10.0; box2.breadth = 11.0; box2.height = 12.0; // 计算 box1 的 volumn volumn = box1.length * box1.breadth * box1.height; cout << "box1 的体积 volumn1 = " << volumn << endl; // 计算 box2 的 volumn volumn = box2.length * box2.breadth * box2.height; cout << "box2 的体积 volumn2 = " << volumn << endl; system("pause"); return 0; }
输出:
类中的成员函数:
#include <iostream> using namespace std; // 声明一个盒子类 class Box { public: double length; // 长 double breadth; // 宽 double height; // 高 // 成员函数声明 double getVolumn(void); void setLength(double len); void setBreadth(double bre); void setHeight(double hei); }; // 各类成员函数的实现 double Box::getVolumn(void) { return length * breadth * height; } void Box::setLength(double len) { length = len; } void Box::setBreadth(double bre) { breadth = bre; } void Box::setHeight(double hei) { height = hei; } // 主函数 int main() { // 定义 Box 类对象 Box box1; Box box2; // 声明体积 volumn double volumn = 0.0; // 实例化 box1 box1.setLength(5.0); box1.setBreadth(6.0); box1.setHeight(7.0); // 实例化 box2 box2.setLength(10.0); box2.setBreadth(11.0); box2.setHeight(12.0); // 输出 box1 的体积 volumn1 cout << "box1 的体积 volumn1 = " << box1.getVolumn() << endl; // 输出 box2 的体积 volumn2 cout << "box2 的体积 volumn2 = " << box2.getVolumn() << endl; system("pause"); return 0; }
输出:
面向对象之封装public:
#include <iostream> using namespace std; // 声明一个 Line 类 class Line { public: double len; void setLen(double length); double getLen(void); }; // 各类成员函数的实现 void Line::setLen(double length) { len = length; } double Line::getLen(void) { return len; } // 主函数 int main() { Line line; // 直接访问 line.len = 10.0; cout << "直接访问:长度 = " << line.len << endl; // 间接访问 line.setLen(20.0); cout << "间接访问:长度 = " << line.getLen() << endl; system("pause"); return 0; }
输出:
面向对象之封装:protected
#include <iostream> using namespace std; // 声明一个 Box 类 class Box { protected: double width; }; // 声明 Box 类的子类 SmallBox class SmallBox :Box { public: double getWidth(void); void setWidth(double wid); }; // 函数实现 double SmallBox::getWidth(void) { return width; // 因为 SmallBox 是 Box 的子类,这里直接使用 width } void SmallBox::setWidth(double wid) { width = wid; } // 主函数 int main() { SmallBox smallBox; smallBox.setWidth(11.11); cout << "width = " << smallBox.getWidth() << endl; system("pause"); return 0; }
输出:
继承:基类分别是public、protected、private的情况
继承中的特点
有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性。
-
1.public 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:public, protected, private
-
2.protected 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:protected, protected, private
-
3.private 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:private, private, private
但无论哪种继承方式,上面两点都没有改变:
-
1.private 成员只能被本类成员(类内)和友元访问,不能被派生类访问;
-
2.protected 成员可以被派生类访问。
public 继承:
#include<iostream> #include<assert.h> using namespace std; class A{ public: int a; A(){ a1 = 1; a2 = 2; a3 = 3; a = 4; } void fun(){ cout << a << endl; //正确 cout << a1 << endl; //正确 cout << a2 << endl; //正确 cout << a3 << endl; //正确 } public: int a1; protected: int a2; private: int a3; }; class B : public A{ public: int a; B(int i){ A(); a = i; } void fun(){ cout << a << endl; //正确,public成员 cout << a1 << endl; //正确,基类的public成员,在派生类中仍是public成员。 cout << a2 << endl; //正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。 cout << a3 << endl; //错误,基类的private成员不能被派生类访问。 } }; int main(){ B b(10); cout << b.a << endl; cout << b.a1 << endl; //正确 cout << b.a2 << endl; //错误,类外不能访问protected成员 cout << b.a3 << endl; //错误,类外不能访问private成员 system("pause"); return 0; }
protected 继承
#include<iostream> #include<assert.h> using namespace std; class A{ public: int a; A(){ a1 = 1; a2 = 2; a3 = 3; a = 4; } void fun(){ cout << a << endl; //正确 cout << a1 << endl; //正确 cout << a2 << endl; //正确 cout << a3 << endl; //正确 } public: int a1; protected: int a2; private: int a3; }; class B : protected A{ public: int a; B(int i){ A(); a = i; } void fun(){ cout << a << endl; //正确,public成员。 cout << a1 << endl; //正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。 cout << a2 << endl; //正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。 cout << a3 << endl; //错误,基类的private成员不能被派生类访问。 } }; int main(){ B b(10); cout << b.a << endl; //正确。public成员 cout << b.a1 << endl; //错误,protected成员不能在类外访问。 cout << b.a2 << endl; //错误,protected成员不能在类外访问。 cout << b.a3 << endl; //错误,private成员不能在类外访问。 system("pause"); return 0; }
private 继承
#include<iostream> #include<assert.h> using namespace std; class A{ public: int a; A(){ a1 = 1; a2 = 2; a3 = 3; a = 4; } void fun(){ cout << a << endl; //正确 cout << a1 << endl; //正确 cout << a2 << endl; //正确 cout << a3 << endl; //正确 } public: int a1; protected: int a2; private: int a3; }; class B : private A{ public: int a; B(int i){ A(); a = i; } void fun(){ cout << a << endl; //正确,public成员。 cout << a1 << endl; //正确,基类public成员,在派生类中变成了private,可以被派生类访问。 cout << a2 << endl; //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。 cout << a3 << endl; //错误,基类的private成员不能被派生类访问。 } }; int main(){ B b(10); cout << b.a << endl; //正确。public成员 cout << b.a1 << endl; //错误,private成员不能在类外访问。 cout << b.a2 << endl; //错误, private成员不能在类外访问。 cout << b.a3 << endl; //错误,private成员不能在类外访问。 system("pause"); return 0; }