这节课继续讲解了 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 = ) : i() { ++number; }
static int getNumber (); //名称混编: geti@Integer__v ()
}; int Integer::getNumber () { //不需要写成static int Integer::getNumber
//++i; 无法在静态成员函数中访问非静态成员
// 非静态成员函数只能在静态成员函数中访问
//this->i++;
// 静态成员函数中无 this 指针
return number;
} int Integer::number = ;//Definition int main () { Integer Zhao, jin, wei, shi, tian, cai; Zhao.i = ;
Zhao.number = ; cout << Zhao.i << endl; cout << Zhao.getNumber () << endl;
cout << Integer::getNumber()<< endl; return ;
}

以下是 组合 的例子

组合就是一个 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 ;
}

然后着重介绍了继承的相关概念

继承就是一个 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", , true);
Student st ("Ph.D Zhao", "CHINA", , false, ); //cout << st.getAge () << endl;
st.addAge ();
//ps.addAge ();
cout << st.getName () << " " << st.getAge () << endl; //派生类对象继承了基类中成员
print (&st); return ;
}

面向对象程序设计-C++ Type conversion (Static) & Inheritance & Composition【第十二次上课笔记】的更多相关文章

  1. 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】

    写在前面: 我记得也不全,如果有记录的更全的同学可以留言,我会添加哒 :) 常量 内敛函数 为什么需要内敛函数 内敛函数适用于什么场合 内敛函数本身,最大优点是,避免了真正函数调用的开销 因为普通函数 ...

  2. 面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】

    这是本门<面向对象程序设计>课最后一次上课,刚好上完了这本<Thinking in C++> :) 这节课首先讲了流 Stream 的概念 平时我们主要用的是(1)在屏幕上输入 ...

  3. 面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】

    本次上课继续讲解了 [ ] .-> 等运算符重载的具体例子 也讲解了C++单个参数的类的类型转换的案例 最后稍微提到了 static 的第三种作用:静态数据成员 具体详解我都已注释出来了,大家可 ...

  4. 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】

    Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: Shape * p1; //使用指针创建对象的方法 p = new Circle (2.0); Sh ...

  5. 20175333曹雅坤 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...

  6. Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?

    什么是面向对象程序设计? 我们称为OOP(Object  Oriented  Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...

  7. Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)

    继承(inheritance)背后的核心思想是:       bonus = b;    }      } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...

  8. 201871010132-张潇潇-《面向对象程序设计(java)》第六-七周学习总结

    201871010132-张潇潇-<面向对象程序设计(java)>第六-七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...

  9. [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程

    [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程 本节导读:本节主要介绍什么是.NET反射特性,.NET反射能为我们做些什么,最后介绍几种常用的 ...

随机推荐

  1. 转移服务器,DEDE网站遇到no input file specified!

    公司新配置了服务器,需要从旧服务器上把原来的站点迁移到新服务器,迁移.NET网站没有遇到任何问题,但是在迁移过来一个用DEDE做的网站后,访问首页出现No Input File Specified,后 ...

  2. linux cpu亲和性设置

    1.设置进程的cpu亲和性程序如下, #Using:g++ setCpuAffinity.c -o setCpuAffinity #include<sys/types.h> #includ ...

  3. HDU 1089 A+B for Input-Output Practice (I)

    #include <cstdio> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) prin ...

  4. Adobe Flash Player已经终止一项可能不安全的操作,解决方案

    http://www.macromedia.com/support/documentation/cn/flashplayer/help/settings_manager04.html

  5. NeHe OpenGL lession 4

    // lession4.c #include <OpenGL/OpenGL.h> #include <GLUT/GLUT.h> #include <stdio.h> ...

  6. uva 1561 - Cycle Game(推理)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style=""& ...

  7. JavaScript中的字符串

    JavaScript字符串是JavaScript最重要的部分,可能比任何其他的数据类型都更多的用到. 所有的JavaScript对象共享的方法之一就是toString(). 字符串对象叫做String ...

  8. 重启IIS报错:IIS 服务或万维网发布服务,或者依赖这 服务可能在启动期间发生错误或者已禁用

    参考文章: http://www.cnblogs.com/zengen/archive/2010/10/29/1864569.html 开启如下服务: Net.Msmq Listener Adapte ...

  9. 自定义cell相关注意事项

    1.拖线成功后,如果又在.h文件或者.m文件里面删除了对应的属性或者方法.一定要在xib文件中,删除关联.方法是:右键点击一下对应的UI控件,把多余的关联叉掉就行了.  不然容易崩溃.

  10. C# Best Practices - Handling Strings

    Features Strings Are Immutable. A String Is a Reference Type Value Type Store their data directly Ex ...