c++入门构造函数

一种方法:

 1 class Student{
 2     public:
 3         //声明有默认参数的构造函数
 4         Student(int=10,int=10,float=10);
 5         //声明成员函数 
 6         void total();
 7     private:
 8         //声明成员变量 
 9         int num;
10         int age;
11         float score;
12         }; 
13 //定义构造函数 
14 Student::Student(int n,int a,float s){
15     num=n;
16     age=a;
17     score=s;
18 }

另一种定义方法:

class Student{
    public:
        //定义构造函数
        Student(int n,int a,float s){
            num=n;
            age=a;
            score=s;
            }
        //声明成员函数 
        void total();
    private:
        //声明成员变量 
        int num;
        int age;
        float score;
        };                 

还有一种方法:

class Student{
    public:
        //定义构造函数
        Student(int n,int a,float s):num(n),age(a),score(s){}
        //声明成员函数 
        void total();
    private:
        //声明成员变量 
        int num;
        int age;
        float score;
        }; 
posted @ 2019-10-07 13:57  沐辰雨  阅读(215)  评论(0编辑  收藏  举报