zoukankan      html  css  js  c++  java
  • 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;
            }; 
  • 相关阅读:
    BZOJ
    BZOJ
    BZOJ
    BZOJ
    BZOJ
    BZOJ
    [知识点]平衡树之Splay
    [BZOJ1015/JSOI2008]星球大战
    [知识点]状态压缩DP
    [NOIP2011]聪明的质检员
  • 原文地址:https://www.cnblogs.com/muchenyu/p/11630219.html
Copyright © 2011-2022 走看看