C++ 教程 在线

2004cpp-constructor-destructor

初始化列表的成员初始化顺序:

C++ 初始化类成员时,是按照声明的顺序初始化的,而不是按照出现在初始化列表中的顺序。

class CMyClass {
    CMyClass(int x, int y);
    int m_x;
    int m_y;
};
CMyClass::CMyClass(int x, int y) : m_y(y), m_x(m_y)
{
};

你可能以为上面的代码将会首先做 m_y=I,然后做 m_x=m_y,最后它们有相同的值。但是编译器先初始化 m_x,然后是 m_y,,因为它们是按这样的顺序声明的。结果是 m_x 将有一个不可预测的值。有两种方法避免它,一个是总是按照你希望它们被初始化的顺序声明成员,第二个是,如果你决定使用初始化列表,总是按照它们声明的顺序罗列这些成员。这将有助于消除混淆。

2003cpp-constructor-destructor

构造函数应用实例:

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
    string name;
    string number;
    char X;
    int year;
    Student(string,string,char,int);      //构造函数声明
    void xianshi(void);     //用于输出类成员的值
};
//成员函数定义,包括构造函数
Student::Student(string N,string n,char x,int y)    //利用构造函数给类的成员赋值
{
    name = N;
    number = n;
    X = x;
    year = y;
}
void Student::xianshi()     //输出成员的值
{
    cout<<name<<endl;
    cout<<number<<endl;
    cout<<X<<endl;
    cout<<year<<endl;
}
int main()                             //主函数
{
    cout<<"输入姓名:";
    string N;
    cin>>N;
    cout<<"输入学号:";
    string n;
    cin>>n;
    cout<<"输入性别(M 或 W):";
    char x;
    cin>>x;
    cout<<"输入年龄:";
    int y;
    cin>>y;
    Student S(N,n,x,y);               //定义对象并对构造函数赋值
    S.xianshi();                           //引用输出函数
    return 0;
}

2002cpp-class-access-modifiers

如果继承时不显示声明是 private,protected,public 继承,则默认是 private 继承,在 struct 中默认 public 继承

class B : A {};
B b;
b.a;    //错误
b.a1;   //错误
b.a2;   //错误
b.a3;   //错误

总结一下三种继承方式:

继承方式 基类的public成员 基类的protected成员 基类的private成员 继承引起的访问控制关系变化概括
public继承 仍为public成员 仍为protected成员 不可见 基类的非私有成员在子类的访问属性不变
protected继承 变为protected成员 变为protected成员 不可见 基类的非私有成员都为子类的保护成员
private继承 变为private成员 变为private成员 不可见 基类中的非私有成员都称为子类的私有成员

2001cpp-class-access-modifiers

在类里面不写是什么类型,默认是 private 的。

include <iostream>
using namespace std;
class Line{
    int a;
};
int main() {
    Line line;
    line.a = 5;
    cout<<line.a<<endl;
}

这个是会报错的,应该改成:

class Line{
    public:
    int a;
};

2000cpp-class-member-functions

学生成绩录入实例:

#include <iostream> 
#include <iomanip>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;
class student
{
    public:
        char name[20];
        char sex[10];
        float math;
        float english;
        float cprogram;
        void input_name();
        void input_sex();
        void input_math();
        void input_english();
        void input_cprogram();
        void input(class student *stu);
        void show_student_massage(class student *stu);
};
void student::input_name()
{
    cout << "输入学生姓名: " << endl;
    cin.getline(name,sizeof(name));
    cout << "学生姓名 : "<< name << endl;
}
void student::input_sex()
{
    cout << "输入学生性别: " << endl;
    cin.getline(sex,sizeof(sex));
}
void student::input_math()
{
    cout << "输入学生数学: " << endl;
    cin >> math;
}
void student::input_english()
{
    cout << "输入学生英语: " << endl;
    cin >> english;
}
void student::input_cprogram()
{
    cout << "输入学生C语言: " << endl;
    cin >> cprogram;
}
void student::show_student_massage(class student *stu)
{
    cout << "学生姓名 : "<< stu->name << endl;
    cout << "学生性别 : "<< stu->sex << endl;
    cout << "学生数学 : "<< stu->math << endl;
    cout << "学生英语 : "<< stu->english << endl;
    cout << "学生C语言: "<< stu->cprogram << endl;
}
void student::input(class student *stu)
{
    stu->input_name();
    stu->input_sex();
    stu->input_math();
    stu->input_english();
    stu->input_cprogram();
}
int main()
{
    student xiaoming;
    student *xiaoming_point = &xiaoming;
    xiaoming.input(xiaoming_point);
    xiaoming.show_student_massage(xiaoming_point);
    return 0;
}

测试结果:

输入学生姓名: 
RUNOOB
学生姓名 : RUNOOB
输入学生性别: 
男
输入学生数学: 
89
输入学生英语: 
98
输入学生C语言: 
79
学生姓名 : RUNOOB
学生性别 : 男
学生数学 : 89
学生英语 : 98
学生C语言: 79