标签:
这节课继续讲解了 static 作为静态数据成员 / 成员函数的用法
具体详解我都已注释出来了,大家可以慢慢看
有任何问题都可以在这篇文章下留言我会及时解答 :)
//static 静态数据成员 //static 静态成员函数 #include <iostream> using namespace std; class Integer { public: int i; static int number; //Declaration, 整个类只有一个版本,所有对象共享 //const static int number = 49; 在C++中也可以这样定义,不过比较奇葩 int geti () { return i; } //名称混编: _geti@Integer_ _v (Integer * const this) Integer (int k = 0) : i(42) { ++number; } static int getNumber (); //名称混编: geti@Integer__v () }; int Integer::getNumber () { //不需要写成static int Integer::getNumber //++i; 无法在静态成员函数中访问非静态成员 // 非静态成员函数只能在静态成员函数中访问 //this->i++; // 静态成员函数中无 this 指针 return number; } int Integer::number = 0;//Definition int main () { Integer Zhao, jin, wei, shi, tian, cai; Zhao.i = 78; Zhao.number = 4; cout << Zhao.i << endl; cout << Zhao.getNumber () << endl; cout << Integer::getNumber()<< endl; return 0; }
以下是 组合 的例子
组合就是一个 has a 的关系,非常好理解
/************************************************************************* > File Name: Code05.cpp > Author: Jeremy Wu > Created Time: Mon 18 May 2015 10:47:03 AM CST ************************************************************************/ #include <iostream> #include <string> using namespace std; //class Building; //类的前置声明 //Building *bd; //前置声明无法创建对象,但可以创建指针 class Building { }; class Student { public: int xuehao; double chengji; string address; }; class Campus { //relation : A Campus has Building & Student Building bd; Student st; }; int main (){ return 0; }
然后着重介绍了继承的相关概念
继承就是一个 is -a 的关系
/************************************************************************* > File Name: Code06.cpp > Author: Jeremy Wu > Created Time: Mon 18 May 2015 10:55:00 AM CST ************************************************************************/ //构造函数调用顺序 //先调用基类构造函数 //再创建成员 //最后调用派生类构造函数 #include <iostream> #include <string> using namespace std; class Person { //Base class private: string name, address; bool sex; protected: //Protected, like private, but is avaiable in derived calss int age; public: int getAge () { return age; } void setAge (int i) { age = i; } string getName () { return name; } void setName (string nm) { name = nm; } string getAdress () { return address; } void setAdress (string ar) { address = ar; } bool getSex () { return sex; } void setSex (bool sx) { sex = sx; } Person (string nm, string ar, int a, bool s) : name (nm), address (ar), age (a), sex (s) { cout << "Person (string nm, string ar, int a, bool s) is called" << endl; } ~Person () { cout << "~Person () is called" << endl; } }; class Test { public: Test () { cout << "Test () is called" << endl; } ~Test () { cout << "~Test () is called" << endl; } }; class Student : private Person { //Derived class //relation : A student is a person //if private derived, all the public funcions will be private as well as member varible //Person ps; Test t; unsigned int id; public: int getId () { return id; } int setId (unsigned int id) { this->id = id; } void addAge () { ++age; } //void addAge () { setAage (getAge () + 1 ); } //low efficiency Student (string nm, string ar, int a, bool s, unsigned int xh) : Person (nm, ar, a, s), id (xh) { cout << "Student (string nm, string ar, int a, bool s, unsigned int xh) is called" << endl; } ~Student () { cout << "~Student () is called" << endl; } }; class UniversityStudent : public Student { //void print () { getName (); } 基类需要保护继承 }; void print (Person *p) { cout << p->getAge () << endl << p->getName () << endl << p->getAdress () << endl << p->getSex () << endl; //<< p->setId () << endl; //Object slice 对象剪切 } int main () { Person ps ("Zhao jinwei", "U.S.A", 20, true); Student st ("Ph.D Zhao", "CHINA", 20, false, 1322); //cout << st.getAge () << endl; st.addAge (); //ps.addAge (); cout << st.getName () << " " << st.getAge () << endl; //派生类对象继承了基类中成员 print (&st); return 0; }
面向对象程序设计-C++ Type conversion (Static) & Inheritance & Composition【第十二次上课笔记】
标签:
原文地址:http://www.cnblogs.com/wushuaiyi/p/4511779.html