
目录
静态:
1. 函数结束时,静态变量不会被破坏.
2. 限制标识符范围不能扩展
3. 静态成员只能初始化一次.

您不能使用自动参数列表进行初始化. (仅用于分配值)因为静态成员属于类而不是对象,所以类中的所有对象共享同一静态成员. 单个对象无权初始化静态变量. 如果单个对象有权初始化静态变量,则将构造每个对象,静态成员将被初始化一次,并且该静态成员将被重复初始化.
class A{
int n;
static int m;
public:
A(){
n=0;
m=0;//right 赋值。而不是初始化
cout<<"无参构造"<<endl;
}
A(int n,int m):n(n),m(m){//error 共用的静态成员被反复初始化
cout<<"有参构造"<<endl;
}
int A::m=111;类的静态成员只能够在类外去初始化。 (类型名 类名::静态成员变量名)并且,类的静态成员必须先初始化,然后才能赋值,否则无法编译。
一个类只有一个对象. 利用静态的特性来实现.
不允许静态成员函数使用const来限制函数中成员变量的修改. 静态成员函数的返回值必须是静态类型的静态成员函数. 不允许使用此指针. (此指针是类的对象的第一个地址. 它是对象的成员的地址,直到第一个地址+偏移量为止,并且静态成员函数对所有对象都是公用的,因此无法通过该函数获得其地址指针. )函数中不允许使用静态成员访问(读访问和写访问)非静态成员变量
三个面向对象的功能: 封装(私有公开以使类成员更安全),继承(以提高开发速度,提高代码重用性)和多态性

为什么要继承?
出于软件工程的需要,
高级聚类: 类应具有尽可能多的特征(即成员变量和成员函数). 只有当特征太多时,差异才会出现.
低耦合: 不同类之间的相同特性称为耦合. 不同类别之间的相同特征应尽可能少. 耦合度越低,工作中的无用消耗就越少.
如何继承?

class Parent{
};
class Child:继承方式 Parent{
};
继承方式:
public
private
protected
继承的类: 父类的基类
继承的类: 子类的派生类
在现实生活中: 派生类属于基类,但是在数字世界中,派生类对象包含基类对象,因为派生类具有更多特征. 派生类对象中有一个基类对象,它是基类对象的成员,并且包含派生类对象.
#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class horse{
protected:
string name;
int speed;
public:
horse();
void show();
};
horse::horse(){
name="无名";
speed=5;
}
void horse::show(){
cout<<"这是一匹叫做"<<name<<"的马."<<"时速"<<speed<<"迈。"<<endl;
}
class WhiteHorse:public horse{
int speed;//派生类中若有和基类相同的成员,在派生类的对象分配的内存中两者所处的位置是不同,不会覆盖。
public:
void eat();
};
void WhiteHorse::eat(){
cout<<"有一匹名叫"<<name<<"的马在悠闲的吃草";
}
int main(int argc, char** argv) {
horse h;
WhiteHorse wh;
wh.show();
//wh之所以能调用show函数,是因为内存中对象h中有的成员,wh中全部包括(并未顺序位置相同),对象名自带this指针和偏移量,所以能够调用show函数。 (继承的底层实现)
h.show();
wh.eat();
return 0;
}

派生类的成员可以访问基类的公共成员和受保护成员. 基类的私有成员,派生类的成员函数不可访问. 如果将私有成员更改为公共成员,则主要功能可以再次访问该成员,并且受保护的成员应运而生. 只能由派生类的成员函数和基类的成员函数访问.
#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class horse{
public:
string name;
int speed;
public:
horse();
void show();
};
horse::horse(){
name="无名";
speed=5;
}
void horse::show(){
cout<<"这是一匹叫做"<<name<<"的马."<<"时速"<<speed<<"迈。"<<endl;
}
class WhiteHorse:public horse{
public:
string name;
int speed;
void eat();
};
void WhiteHorse::eat(){
cout<<"有一匹名叫"<<name<<"的马正在悠闲的吃草"<<endl;
}
int main(int argc, char** argv) {
horse h;
WhiteHorse wh;
h.name="汗血马";
h.speed=10000;
wh.name="白龙马";
wh.speed=5000;
wh.horse::name="汗血马的儿子";
//调用时如何区分派生类对象中的名字相同的基类成员和派生类成员:通过类名加作用域运算符区分。
wh.horse::speed=1000;
wh.show();
h.show();
wh.eat();
return 0;
}
派生类对象如何构造基类成员?
通过调用基类构造函数来构造派生类对象中的基类成员.
#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class horse{
public:
string name;
int speed;
public:
horse(string name,int speed):name(name),speed(speed){};
horse();
void show();
};
horse::horse(){
name="无名";
speed=5;
}
void horse::show(){
cout<<"这是一匹叫做"<<name<<"的马."<<"时速"<<speed<<"迈。"<<endl;
}
class WhiteHorse:public horse{
public:
string name;
int speed;
void eat();
WhiteHorse(int whSpeed,string whname,int speed,string name):speed(whSpeed),name(whname),horse(name,speed){};
};
/*
WhiteHorse::WhiteHorse(int whSpeed,string whname,int speed,string name){
speed=whSpeed;
name=whname;
/*horse::name=name;
horse::speed=speed;//error
horse(name,speed);//error
}*/
void WhiteHorse::eat(){
cout<<"有一匹名叫"<<name<<"的马正在悠闲的吃草"<<endl;
}
int main(int argc, char** argv) {
WhiteHorse wh(100,"小小马",10000,"老子马");
wh.show();
cout<<wh.horse::name<<endl;
cout<<wh.horse::speed<<endl;
cout<<wh.name<<endl;
cout<<wh.speed<<endl;
return 0;
}
私有: 受保护: 公开:
在继承方法和封装方法中,选择更安全的一种. (您不必担心这个细节c 类 static 函数,只需记住这种词即可)
一个类可以被多个类继承. 一个类可以继承多个类. 您不能直接或间接继承自己
如果是菱形继承,就会出现歧义问题c 类 static 函数,请使用虚拟继承来解决. 虚拟继承创建虚拟基类的表. 该表是一个指针,其中包含虚拟基类的偏移量. 然后,子类中的多个父对象都是虚拟基类表中的所有对象.
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-245729-1.html
同我们一样
如果没有蛆