v Student h class Student public Student int
类与对象 v 程序示例—— 类声明 (Student. h) class Student { public: Student(); // 构造方法 int Get. Roll. No(); // 获取学号 void Set. Roll. No(int); // 设置学号 char* Get. Name(); // 获取姓名 void Set. Name(char*); // 设置姓名 private: int roll. No; // 学号 char* name; // 姓名 };
类与对象 v 程序示例 — 类方法的实现 (Student. cpp) #include “Student. h” #include <string. h> Student : : Student() { strcpy(this -> name, “”); // 给 name 属性赋初值 this->roll. No = 0; // 给 roll. No 属性赋初值 } int Student : : Get. Roll. No() { return this->roll. No; }
类与对象 v 程序示例 — 类方法的实现 (Student. cpp) void Student : : Set. Roll. No(int no) { if (no>=0) this->roll. No = no; } char * Student : : Get. Name() { return this->name; } void Student : : Set. Name(char* s) { strcpy(this->name, s); }
类与对象 v 程序示例 —— 类的使用(Test. cpp) int main(){ Student stu, * p_st; stu. Set. Name(“Li Ming”); stu. Set. Roll. No(1); p_st = new Student; p_st -> Set. Name(“Zhang Qiang”); p_st -> Set. Roll. No(2); return 0; }
程的结构 Student 类的声明 Student. h Student 类的实现 Student. cpp main() 函数 Test. cpp
访问权限 class Student { public: Student(); int Get. Roll. No(); void Set. Roll. No(int); char * Get. Name(); void Set. Name(char*); private: int roll. No; char * name; }; int main () { Student stu; stu. roll. No=2; return 0; }; ✗ int main () { Student stu; stu. Set. Roll. No(2); return 0; }; ✓
继承的例子 class Shape virtual double Cal. Area(); class Rectangle double width; double height; Rectangle(double, double); double Get. Width(); void Set. Width(double); double Get. Height(); void Set. Height(double); double Cal. Area(); class Circle double radius; Circle(double); double Get. Radius(); void Set. Radius(double); double Cal. Area();
继承的例子 class Shape{ public: Shape(); virtual double Cal. Area(); }; Shape : : Shape(){ } double Shape : : Cal. Area(){ return 0; }
继承的例子 class Rectangle : public Shape { public: Rectangle(double, double); double Get. Width(); void Set. Width(double); double Set. Height(); void Set. Height(double); double Cal. Area(); private: double height; double width; };
继承的例子 Rectangle: : Rectangle(double w, double h) : width(w), height(h) { } double Rectangle: : Get. Width() { return width; } void Rectangle: : Set. Width(double w) { width = w; }
继承的例子 double Rectangle: : Get. Height() { return height; } void Rectangle: : Set. Height(double h) { height = h; } double Rectangle: : Cal. Area() { return width*height; }
继承的例子 class Circle : public Shape { public: Circle(double); double Get. Radius(); void Set. Radius(double); double Cal. Area(); private: double radius; };
继承的例子 #define PI 3. 14159 Circle: : Circle(double r) : radius(r) { } double Circle: : Get. Radius(){ return radius; } void Circle: : Set. Radius(double r) { radius = r; } double Circle: : Cal. Area(){ return PI*radius; }
多态性的例子 #include “Shape. h” #include “Rectangle. h” #include “Circle. h” #include <iostream. h> 23. 1 int main() { 28. 27 Shape * s 1 = new Rectangle(4. 2, 5. 5); Shape * s 2 = new Circle(3); cout<<s 1 ->Cal. Area()<<endl; cout<<s 2 ->Cal. Area()<<endl; return 0; }
- Slides: 31