面向对象程序设计-C++ Type conversion (Static) & Inheritance & Composition【第十二次上课笔记】
这节课继续讲解了 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【第十二次上课笔记】的更多相关文章
- 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】
写在前面: 我记得也不全,如果有记录的更全的同学可以留言,我会添加哒 :) 常量 内敛函数 为什么需要内敛函数 内敛函数适用于什么场合 内敛函数本身,最大优点是,避免了真正函数调用的开销 因为普通函数 ...
- 面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】
这是本门<面向对象程序设计>课最后一次上课,刚好上完了这本<Thinking in C++> :) 这节课首先讲了流 Stream 的概念 平时我们主要用的是(1)在屏幕上输入 ...
- 面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】
本次上课继续讲解了 [ ] .-> 等运算符重载的具体例子 也讲解了C++单个参数的类的类型转换的案例 最后稍微提到了 static 的第三种作用:静态数据成员 具体详解我都已注释出来了,大家可 ...
- 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】
Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: Shape * p1; //使用指针创建对象的方法 p = new Circle (2.0); Sh ...
- 20175333曹雅坤 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...
- Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?
什么是面向对象程序设计? 我们称为OOP(Object Oriented Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...
- Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)
继承(inheritance)背后的核心思想是: bonus = b; } } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...
- 201871010132-张潇潇-《面向对象程序设计(java)》第六-七周学习总结
201871010132-张潇潇-<面向对象程序设计(java)>第六-七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...
- [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程
[.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程 本节导读:本节主要介绍什么是.NET反射特性,.NET反射能为我们做些什么,最后介绍几种常用的 ...
随机推荐
- A Byte of Python 笔记(6)模块
第8章 模块 用户在程序中定义一次函数而重用代码,如果用户想在其他程序中重用很多函数,可以通过使用模块的方式. 模块就是一个包含了所有用户定义的函数和变量的文件.为了在其他程序中重用模块,模块的文件名 ...
- ASP.NET MVC5 学习笔记-4 OWIN和Katana
1. Owin OWIN全名:Open Web Interface for .NET. 它是一个说明,而非一个框架,该声明用来实现Web服务器和框架的松耦合.它提供了模块化.轻量级和便携的设计.类似N ...
- GC算法之串行并行并发
串行收集器: 用单线程处理所有垃圾回收工作,因为无需多线程交互,所以效率比较高.但是,也无法使用多处理器的优势,所以此收集器适合单处理器机器.当然,此收集器也可以用在小数据量(100M左右)情况下的多 ...
- Java web 开发环境配置。
一.配置 win8 64位 环境java 开发环境 1. 下载JDK,地址 http://www.oracle.com/technetwork/java/javase/downloads/index ...
- HDU 2719 The Seven Percent Solution
#include <cstdio> #include <cstring> int main() { ]; ]!='#') { ; while (i<strlen(s)) ...
- HDOJ 1427(dfs) 速算24点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...
- ADO.NET(一)
- 使用PowerShell 命令集进行SQL Server 2012 备份和还原
最近心相不错,所以打算翻译一些英文文档做福利,原文在此,翻译有不足的地方还请各位兄弟指点. 讨论什么是DBA最重要的工作的时候,你最常听到就是一条就是DBA只要做好备份和恢复.事实如此,如果你不做备份 ...
- iOS获取运营商信号强度
此API是apple私有API,所以只可运用在越狱设备中,如果提交appstore,会遭遇apple的拒绝上架反馈! #import <dlfcn.h> int getSignalLeve ...
- C# Best Practices - Creating Good Properties
Coding Properties Code in the Getter Check the user's credentials Check application state Format the ...