面向对象程序设计-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反射能为我们做些什么,最后介绍几种常用的 ...
随机推荐
- PHP的排列组合问题 分别从每一个集合中取出一个元素进行组合,问有多少种组合?
首先说明这是一个数学的排列组合问题C(m,n) = m!/(n!*(m-n)!) 比如:有集合('粉色','红色','蓝色','黑色'),('38码','39码','40码'),('大号','中号') ...
- Ubuntu在ARM上建立NFS服务
先引用别人的做法: 1.进行NFS服务器端与客户端的安装: sudo apt-get install nfs-kernel-server nfs-common portmap 安装客户端的作用是可以在 ...
- Php 使用 fsockopen发送http请求
<?php function HTTP_Post($URL,$data, $referrer="") { // parsing the given URL $URL_Info ...
- 自制的七个C,总结的太好了
拿破仑·希尔把它叫做:“自制的七个C”: 1.控制自己的时间(Clock). 时间虽不断流逝,但也可以任人支配.你可以选择时间来工作.游戏.休息.烦恼..虽然客观的环境不一定能任人掌握,但人却可以自己 ...
- Objective-c 程序结构
类是Objective-c的核心,Objective-c程序都是围绕类进行的.Objective-c程序至少包含以下三个部分: 1.类接口:定义了类的数据和方法,但是不包括方法的实现代码. 2.类实现 ...
- SQL Server调试常用参数
SQL Server 清除缓存: DBCC DROPCLEANBUFFERS 从缓冲池中删除所有清除缓冲区. DBCC FREEPROCCACHE 从过程缓存中删除所有元素. DBCC FREESYS ...
- 阿里Android一面(校招)
4.8号晚上8点多接到了阿里一面的电话,面试官人很好,和我聊了半个多小时.我面的是无线事业部,就是做淘宝客户端的那个部门.面试问的都很基础,刚开始问了hashmap和快速排序.接着就是问Android ...
- 读书笔记之MERGE 语句使用
常用语法 MERGE INTO <target table> AS TGT USING <SOURCE TABLE> AS SRC ON <merge predicate ...
- Codeforces 18C C. Stripe
Codeforces 18C C. Stripe 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E 题 ...
- ansible不配ssh连接,用户密码登录
ansible 不配ssh免密链接,直接用ssh用户密码连接,要先装sshpass. 否则会报错: sshpass安装 sshpass下载地址:http://sourceforge.net/proje ...