C++

框架:

#include<iostream>

class super
{
public:
    int member;
//构造器,前面不能加void
    super(int member);
    super(const super& another);
//析构器,前面不能加void,基类最好定义为虚方法
    virtual ~super();
//虚方法
    virtual void operation(void);
//重载运算符
    super operator+(super &anthor);
    friend std::ostream& operator<<(std::ostream& os,super another);
protected://这个类和它的子类
private:  //只有这个类
};

super::super(int member)
{
    this->member=member;
}

super::~super()
{

}

void super::operation(void)
{
    std::cout<<"A"<<std::endl;
}

//继承

class sub:public super
{
public:
    int member;
    sub(int member);
    ~sub();
    void operation(void);
protected:
private:
};

sub::sub(int member):super(member){}

sub::~sub(){}

void sub::operation(void)
{
    std::cout<<'B'<<std::endl;
}

//该类为抽象类,不能有实例
class other
{
//友元
    friend class super;
public:
//抽象方法
    virtual void abstract(void)=0;
protected:
private:
};

//抽象方法

class other2:public other
{
public:
    virtual void abstract(void);
};

void other2::abstract(void)
{
    std::cout<<"abstract method"<<std::endl;
}

//重载运算符
super super::operator+(super &anthor)
{
    return super(member+anthor.member);
}
//重载<<运算符
std::ostream& operator<<(std::ostream& os,super another)
{
    os<<another.member;
}
//副本构造器
super::super(const super& another)
{
    this->member=another.member;
}

int main()
{
//虚方法
    super *sub_pointer=new sub(0);
    sub_pointer->operation();
//重载运算符
    super A(1),B(2);
    std::cout<<A+B<<std::endl;
//抽象方法
    other2 C;
    C.abstract();
//delete:删除数组:delete[]
    delete sub_pointer;
    return 0;
}

构造函数:

class super
{
public:
    int member1,member2;
    super(int member1,int member2);
//隐式转换构造函数
    super(const int& member4):member1(member4)
    {

    }
//复制构造函数
    super(const super& other)
    {

    }
};
//初始化列表
super::super(int member1,int member3):
    member1(member1),member2(member3)
{

}
一般声明为public
因为创建对象一般是在外部进行的
若声明为protected或private
那就意味着在外部创建对象是错误的
只能由类内部实例化(不通常的做法)
一般来说:不应同时使用 构造函数的重载 和 带默认参数的构造函数(会产生歧义)
带默认参数的构造函数只能在类内部定义
explicit 前缀定义 构造函数防止 隐式转换,在类外部定义不能重复加explicit

继承中的访问控制:

public:继承会保留基类中成员(包括函数和变量等)的可见性不变
protected:继承会将基类中的public可见性的成员修改成为protected可见性
private:继承会将基类中的public和protected可见性的成员修改成为private可见性

不允许重载的符号:

.  //成员访问运算符
.* //成员指针访问运算符
:: //域运算符
sizeof
?: //条件运算符

成员指针与成员函数指针:

#include<iostream>

class super
{
public:
    int member;
    super(int member)
    {
        this->member=member;
    }
//const限定改函数不能改变成员的值
    int operation(int member)const
    {
        return this->member;
    }
};

int main()
{
//成员指针
    int super::*pointer_member=&super::member;
//成员函数指针
    int (super::*pointer_operation)(int member)const=&super::operation;
    super A(2);
    super *p=&A;
    std::cout<<p->*pointer_member<<std::endl;
    std::cout<<A.*pointer_member<<std::endl;
    std::cout<<(p->*pointer_operation)(3)<<std::endl;
    std::cout<<(A.*pointer_operation)(3)<<std::endl;
    return 0;
}

const的限定:

class super1
{
public:
    int member1;
    super1(int member1):member1(member1){}
//可修改该常对象的值
    mutable int member2;
};
//常对象(2种定义)
//所有数据成员都不能被修改
//只能调用常函数,不能调用非常函数
//不能被非常函数访问,只能被常函数访问
//只能被 指向常对象的指针 指向
const super1 A(2);
super1 const B(2);

class super2
{
//常数据成员
//只能通过构造函数的初始化列表进行初始化
//常函数与非常函数都可以访问,但不能修改
public:
//const数据成员
//既可以被常函数访问也可以被非常函数访问
    const int a;
    int operation(void)const;
    const int operation(void);
};
//常函数
//可以访问常数据成员或非常数据成员
//不能改变数据成员的值
//不能调用非常函数
int super2::operation(void)const
{

}
//返回值为const
const int super2::operation(void)
{

}

super1 C(2);
//指向对象的常指针
//不能修改它的指向
//必须在定义的时候被初始化
super1* const p1=&C;

//指向常对象的指针
//不能通过它改变指向对象的值
//可以改变指向
const super1* p2;

//对象的常引用
const super1& D(2);

类中静态函数与变量:

#include<iostream>
//无需对象即可调用
class super
{
public:
    static int cont;
    static int getcont(void)
    {
        return cont;
    }
};

int super::cont=2;

int main()
{
    std::cout<<super::getcont()<<std::endl;
    return 0;
}

多继承:

class super
{
public:
    int member;
    super(int member)
    {
        this->member=member;
    }
protected:
private:
};

class sub1:public super
{
public:
    int member1;
    sub1(int member,int member1):super(member)
    {
        this->member1=member1;
    }
};

class sub2:public super
{
public:
    int member2;
    sub2(int member,int member2):super(member)
    {
        this->member2=member2;
    }
};

class subsub:public sub1,public sub2
{
public:
    subsub(int member,int member1,int member2):
        sub1(member,member1),sub2(member,member2)
    {

    }
};

虚继承:

class super
{
public:
    int member;
    super(int member)
    {
        this->member=member;
    }
protected:
private:
};

class sub1:virtual public super
{
public:
    int member1;
    sub1(int member,int member1):super(member)
    {
        this->member1=member1;
    }
};

class sub2:virtual public super
{
public:
    int member2;
    sub2(int member,int member2):super(member)
    {
        this->member2=member2;
    }
};

class subsub:public sub1,public sub2
{
public:
    subsub(int member,int member1,int member2):
        sub1(member,member1),sub2(member,member2),super(member)
    {

    }
};

虚函数:

#include<iostream>

//虚函数多态的条件:
//类之间的继承符合赋值兼容
//改写了同名虚函数
//根据赋值兼容规则使用指针或引用
//静态,内联,构造 函数不能是虚函数
class super
{
public:
    virtual int operation(void)
    {
        return 1;
    }
};

class sub:public super
{
public:
//不加virtual也是虚函数
    int operation(void)
    {
        return 2;
    }
};

void print(super *X,int(super::*Y)(void))
{
    std::cout<<(X->*Y)()<<std::endl;
}

int main()
{
    sub A;
    super *B=&A,&C=A;
    std::cout<<B->operation()<<std::endl;
    std::cout<<C.operation()<<std::endl;
    print(&A,&super::operation);
    return 0;
}

重载运算符:

成员函数版:
class super
{
    int member1,member2;
public:
    super(int member1=0,int member2=0):
        member1(member1),member2(member2) {}
    super operator+(super &another);
//前置单目运算符重载:
    super operator++();
//后置单目运算符重载:
    super operator++(int);
    super& operator+=(super& another);
//重载<<和>>(只能用友元函数)
    friend std::ostream& operator<<(std::ostream& os,const super& other);
    friend std::istream& operator>>(std::istream& is,super& other);
//重载强制转换运算符(不需指明返回值类型,默认和转换类型相同)
    operator int();
    void print(void)
    {
        std::cout<<member1<<","<<member2<<std::endl;
    }
};

super super::operator+(super &another)
{
    return super(member1+another.member1,member2+another.member2);
}

super super::operator++()
{
    return super(++member1,++member2);
}

super super::operator++(int)
{
    return super(member1++,member2++);
}

super& super::operator+=(super& another)
{
    member1+=another.member1;
    member2+=another.member2;
    return *this;
}

std::ostream& operator<<(std::ostream& os,const super& other)
{
    os<<other.member1<<","<<other.member2<<std::endl;
    return os;
}

std::istream& operator>>(std::istream& is,super& other)
{
    is>>other.member1>>other.member2;
    return is;
}

super::operator int()
{
    return member1+member2;
}

友元函数版:
class super
{
    int member1,member2;
public:
    super(int member1=0,int member2=0):
        member1(member1),member2(member2){}
    friend super operator+(const super& a,const super& b);
//前置单目运算符重载:
    friend super operator++(super& a);
//后置单目运算符重载:
    friend super operator++(super& a,int);
    void print(void)
    {
        std::cout<<member1<<","<<member2<<std::endl;
    }
};

super operator+(const super& a,const super& b)
{
    return super(a.member1+b.member1,a.member2+b.member2);
}

super operator++(super& a)
{
    return super(++a.member1,++a.member2);
}

super operator++(super& a,int)
{
    return super(a.member1++,a.member2++);
}

模板:

//函数模板
template<typename T,typename U,typename V>
void fun(T param)
{

}

//类模板(带默认形参)
template<class T,class U,int V=2,class W=int>
class super
{
public:
    T member;
    super();
    ~super();
    void operation(T val);
};

template<class T,class U,int V,class W>
super<T,U,V,W>::super()
{

}

template<class T,class U,int V,class W>
super<T,U,V,W>::~super()
{

}

template<class T,class U,int V,class W>
void super<T,U,V,W>::operation(T val)
{

}
定义方式:
super<int,int>A;
super<int,int,5>B;
super<int,int,5,int>C;

内联模板:

//函数模板
template<typename T,typename U,typename V>
inline void fun(T param)
{

}

//类模板
template<class T,class U,int V>
class super
{
public:
    T member;
    super(){}
    ~super(){}
    void operation(T val)
    {

    }
};
模板的使用:
super<int,double,5>X;

命名空间:

//不能用于 函数,结构体,类内部
namespace A
{
    int a;
}

namespace B
{
    int a,b;
    namespace C
    {
        int c,d;
        namespace very_long_name_namespace
        {
            int e,f;
        }
    }
}

//可以没有名字
namespace
{
    
}

//简化名字
namespace D=B::C::very_long_name_namespace;

//可以不连续
namespace A
{
    int b;
}

using std::cout;

using namespace std;

强制转换:

class super
{virtual void operation(void){}};

class sub:public super
{};

int main()
{
    super *A,D;
    sub B,*C;
//动态转换
//必须是指针类型的转换
    A=dynamic_cast<super*>(&B);
//子类转换为基类
//基类必须有虚函数
    C=dynamic_cast<sub*>(&D);
//静态转换
    double fo0Old=2.33333333;
    int fold=static_cast<int>(fo0Old);
    return 0;
}

迭代器:

std::容器类<T>::iterator iter=容器对象.begin();

重载,覆盖,隐藏

重载:
相同的范围(在同一个类中)函数名字相同;
参数不同;
virtual关键字可有可无
覆盖:
不同的范围(分别位于派生类与基类);
函数名字相同
参数相同
基类函数必须有virtual关键字
隐藏:
如果派生类的函数与基类的函数同名,但是参数不同,不论有无virtual关键字,基类的函数将被隐藏
如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual 关键字,此时,基类的函数被隐藏
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy阅读 9,447评论 1 51
  • 重新系统学习下C++;但是还是少了好多知识点;socket;unix;stl;boost等; C++ 教程 | 菜...
    kakukeme阅读 19,425评论 0 50
  • C++对C的加强 0.面向过程–>函数+面向对象–>类和对象 1.namespace命名空间 2.实用性 增加 -...
    钱嘘嘘阅读 572评论 0 2
  • 1. 结构体和共同体的区别。 定义: 结构体struct:把不同类型的数据组合成一个整体,自定义类型。共同体uni...
    breakfy阅读 2,072评论 0 21
  • 1. C++基础知识点 1.1 有符号类型和无符号类型 当我们赋给无符号类型一个超出它表示范围的值时,结果是初始值...
    Mr希灵阅读 17,697评论 3 82