这节课继续讲解了 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. android开发环境安装记录

    首先进入http://developer.android.com/sdk/index.html, Google提供了一个新的DeveloperTools,即:ADT Bundle,中文翻译之:ADT捆 ...

  2. LintCode-最长公共子串

    题目描述: 给出两个字符串,找到最长公共子串,并返回其长度. 注意事项 子串的字符应该连续的出现在原字符串中,这与子序列有所不同. 样例 给出A=“ABCD”,B=“CBCE”,返回 2 public ...

  3. 转: vim简明教程

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  4. Android Studio ADB响应失败解决方法

    当启动Android Studio时,如果弹出 adb not responding. you can wait more,or kill "adb.exe" process ma ...

  5. 【Untiy3D 游戏开发之一】Unity3D For Window/Mac最新4.2.0版本破解教程

    转载请标明:转载自[小枫栏目],博文链接:http://blog.csdn.net/rexuefengye/article/details/11646885 一.Unity3D For Mac 1.首 ...

  6. 实现最小宽度的几种方法及CSS Expression[转]

    实现最小宽度的几种方法及CSS Expression[转] 实现最小宽度的几种方法:css表达式尽量不用 支持FF IE7  IE6 .test { background:blue; min-widt ...

  7. Palindrome(Manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 6183   Accepted: 2270 Descr ...

  8. 人类科技的发展为什么会是加速度的(TRIZ方法再推荐)

    从人类的历史发展来看,近200年来的科技发展的成果超过了过去几千年中科技发展的成果,并且从短时间来看.这样的加速趋势也是很明显的,想想十年前和如今的对照,科技的发展确实是日新月异. 科技的发展固然有偶 ...

  9. [译]Stairway to Integration Services Level 18 – 部署和执行

    介绍 在本文中,我们要创建一个SSIS Catalog 实例,部署我们的项目,并且运行 weather data loader 包. SSIS 2012 部署模型   SSIS 2012 Deploy ...

  10. 前端CSS规范大全

    一.文件规范 1.文件均归档至约定的目录中(具体要求以豆瓣的CSS规范为例进行讲解): 所有的CSS分为两大类:通用类和业务类.通用的CSS文件,放在如下目录中: 基本样式库 /css/core 通用 ...