这节课继续讲解了 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. web附件中文名

    response.setHeader("Content-Disposition", "attachement;filename="+URLEncoder.enc ...

  2. 【转】vs2008中leptonica-1.68安装配置

    tesseract ocr挺不好配置的,找到一篇不错的文章,分享如下:http://hi.baidu.com/ever8936/blog/item/6998e1196b1d0161dab4bd8f.h ...

  3. MVC-04 视图(3)

    五.Url辅助方法 Url辅助方法与HTML辅助方法很类似,HTML辅助方法用来产生HTML标签,而Url辅助方法则负责用来产生Url网址. @Url.Action("About" ...

  4. Android系统设置— android.provider.Settings

    android.provider.Settings Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS); sta ...

  5. C++_template_栈的链式存储及实现

    由于在C++数据结构中的代码不完整,特补全.等日后当工程库调用. 若有疑问,请留言. #include<iostream> using namespace std; template< ...

  6. AlertDialog弹出时背景明暗程度调整

    今天有个需求是把弹出AlertDialog时的变暗的背景调整得不要那么暗. 一开始懒惰就直接百度中文搜索,结果找到的代码试了几次都不行. 后来老老实实开google.stackoverflow搜索,搜 ...

  7. HDOJ Sudoku Killer(dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 思路分析:该问题为数独问题,明显解是唯一的,所有采用dfs搜索效果更好: 在搜索时,可以通过3个 ...

  8. 【cocos2d-x】Win7下配置Cocos2d-x开发环境

    一.下载安装包 先去Cocos2d-x官网下载安装包,最新版本为cocos2d-2.1.5 http://www.cocos2d-x.org/news/134 二.解压安装包 下载完成后,解压文件,解 ...

  9. hdu3416 Marriage Match IV【最短路+最大流】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297581.html   ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...

  10. 感觉挺有意思的SQL题目

    1.有如下数据,要求查询每个班最低分和最高分,并将最高分与最低分显示为同一列 ID Student CourseName Score1 张三 English 802 张三 Math 703 张三 Ch ...