C++学习笔记

1.可以在类外部定义指针成员,指向类内成员,如下:
#include<iostream>
class Sample
{
public:
    int x;
    int y;
    void disp()
    {
        std::cout<<"x="<<x<<",y="<<y<<std::endl;
       
    }
   
};
int main()
{
    int Sample::*pc;

    Sample s;
    pc=&Sample::x;
    s.*pc=10;
    pc=&Sample::y;
    s.*pc=20;
    s.disp();
    return 0;
}
result:
x=10,y=20


2.传值调用 调用拷贝构造函数,创建新的对象,函数结束,对象生命周期结束,调用析构函数
#include<iostream>
using namespace std;
class Sample
{
    int x,y;
public:
        Sample()
        {
            x=y=0;
        }
        Sample(int i,int j)
        {
            x=i;
            y=j;
        }
        void copy(Sample &s);
        void setxy(int i,int j)
        {
            x=i;
            y=j;
        }
        void print()
        {
            cout<<"x="<<x<<",y="<<y<<endl;
        }
};
void Sample::copy(Sample &s)
{
    x=s.x;
    y=s.y;
}
void func(Sample s1,Sample &s2)
{
    s1.setxy(10,20);
    s2.setxy(30,40);
}
int main()
{
    Sample p(1,2),q;
    q.copy(p);
    func(p,q);
    p.print();
    q.print();
    return 0;
}
result:
x=1,y=2
x=30,y=40

3.string
注意在string中对+运算符进行了重载
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s1="week",s2="end";
    cout<<s1<<" "<<s2<<endl;
    s1=s1+s2;
    cout<<s1<<endl;
    return 0;
}
result:
week    end
weekend

4.常成员变量初始化和一般成员变量初始化
class A
{
    int a;
    const int b;
    A():b(0)
    {
        a=0;
    }
}

类不会被分配存储空间,类被实例化为对象之后,才会分配存储空间。

5.类内成员函数 嵌套调用函数
可以在类内成员函数内定义嵌套函数调用

6.虚基类的构造函数调用,成员函数同名覆盖,函数调用优先级
/*1.虚基类构造函数在非虚基类构造函数之前被调用,   2.虚基类的构造函数只由最远派生类构造函数调用一次, 其他派生类的调用都被忽略,
3.当类成员重名时可采用 虚函数,同名覆盖原则, 作用域::运算符
4.函数调用 优先级高于插入符 << 和提取符 >>.
*/
#include<iostream>
using namespace std;
class A
{
public:
    A(int a):x(a)
    {
        cout<<"A constructor..."<<x<<endl;
    }
    int f()
    {
            return ++x;
    }
    ~A()
    {
        cout<<"destructor A..."<<endl;
    }
private:
    int x;
};
class B:public virtual A
{
private:
    int y;
    A Aobj;
public:
    B(int a, int b, int c):A(a),y(c),Aobj(c)
    {
        cout<<"B constructor..."<<y<<endl;
       
    }
    int f()
    {
        A::f();
        Aobj.f();
        return ++y;
    }
    void display()
    {
        cout<<A::f()<<" "<<Aobj.f()<<" "<</*f()<<*/endl;//去掉注释 12 12 11 注释后 11  11 这是因为优先级的问题 函数先调用完成之后,再输出 
    }
    ~B()
    {
        cout<<"destructor B..."<<endl;
    }
};
class C:public B
{
public:
    C(int a, int b, int c):B(a,b,c),A(0)
    {
        cout<<"C constructor..."<<endl;
    }
};
class D:public C//, public virtual A
{
public:
    D(int a, int b, int c):C(a,b,c),A(c)
    {
        cout<<"D contstructor..."<<endl;
    }
    ~D()
    {
        cout<<"destructor D..."<<endl;
    }
};
int main()
{
    D d(7,8,9);
    d.f();
    d.display();
    return 0;
}
result:
A constructor...9
A constructor...9
B constructor...9
C constructor...
D contstructor...
12      12      11
destructor D...
destructor B...
destructor A...
destructor A...

7.只需查看当前派生类继承的基类是否有虚基类,调用当前派生类的基类构造函数进行初始化,


8.通常定义一个以类名为文件名的头文件,在该头文件中实现声明,在以类名文件名的cpp文件中定义该类的实现


9.#ifndef XXX_H
  #define XXX_H
    ....
    ....
    ....
  #endif
避免头文件被重复定义

原文地址:https://www.cnblogs.com/lzh-Linux/p/3507735.html