首页 > 编程语言 > 详细

c++入门构造函数

时间:2019-10-07 14:58:06      阅读:67      评论:0      收藏:0      [点我收藏+]

一种方法:

 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;
        }; 

c++入门构造函数

原文:https://www.cnblogs.com/muchenyu/p/11630219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!