C++语言基础 例程 构造函数

贺老师的教学链接  本课讲解

用构造函数初始化对象

#include <iostream>
using namespace std;
class Time
{
public:
    Time( )
    {
        hour=0;
        minute=0;
        sec=0;
    }
    void set_time( );
    void show_time( );
private:
    int hour;
    int minute;
    int sec;
};
void Time::set_time( )
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}
void Time::show_time( )
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( )
{
    Time t1;
    t1.show_time( );
    Time t2;
    t1.set_time( );
    t2.show_time( );
    return 0;
}

在类外定义构造函数

class Time
{
public:
	 Time( );
	 void set_time( );
	 void show_time( );
  private:
	 int hour;
	 int minute;
	 int sec;
};
Time::Time( )
{
  hour=0;
  minute=0;
  sec=0;
}
带参数的构造函数
class Time
{
public:
    Time();
    Time(int,int,int);
    void show_time();
private:
    int hour;
    int minute;
    int sec;
};
Time::Time()
{
    hour=0;
    minute=0;
    sec=0;
}

Time::Time(int h,int m,int s)
{
     hour=h;
     minute=m;
     sec=s;
}

int main()
{
    Time t1; //不可t1();
    t1.show_time();

    Time t2(15,39,59);
    t2.show_time();
    return 0;
}

用参数初始化表对数据成员初始化
方便、简练的写法:用参数初始化表

class Time
{
public:
   Time( ):hour(0),minute(0),sec(0){};
   Time(int h,int m,int s):hour(h),minute(m),sec(s) {};
   ……
};

也可以在类外用参数初始化表定义构造函数。

Time::Time(int h,int m,int s):hour(h), minute(m), sec(s){};
时间: 2024-07-30 08:49:40

C++语言基础 例程 构造函数的相关文章

C++语言基础 例程 派生类的构造函数和析构函数

贺老师的教学链接  本课讲解 一个简单派生类的定义 #include <iostream> #include<cstring> using namespace std; class Student //声明基类Student { public: Student(int n,string nam,char s):num(n),name(nam),sex(s) {} //基类构造函数 ~Student( ) { } //基类析构函数 void show( ) { cout<<

C++语言基础 例程 默认构造函数

贺老师的教学链接  本课讲解 默认构造函数(default constructor) class Time { public: Time( ); void show_time(); private: int hour; int minute; int sec; }; Time::Time( ) { hour=0; minute=0; sec=0; }

C++语言基础 例程 带默认参数的构造函数

贺老师的教学链接  本课讲解 使用默认参数的构造函数 #include <iostream> using namespace std; class Time { public: Time( ); Time(int h,int m=0,int s=0); void show_time( ); private: int hour; int minute; int sec; }; Time::Time( ) { hour=0; minute=0; sec=0; } Time::Time(int h,

C++语言基础 例程 调用构造函数和析构函数的顺序

贺老师的教学链接  本课讲解 析构函数应用实例方案1 const int N =500; class Salary { public: Salary(int); void input( ); void show( ); ~Salary(); private: double salarys[N]; int number; //实际人数 }; 方案2 class Salary { public: Salary(int); void input( ); void show( ); ~Salary();

C++语言基础 例程 多态性的概念

贺老师的教学链接  本课讲解 一种死板的机制 #include <iostream> #include <string> using namespace std; //声明基类Student class Student { public: Student(int, string,float);//声明构造函数 void display( ); //声明输出函数 protected: //受保护成员,派生类可以访问 int num; string name; float score;

C++语言基础 例程 虚函数

贺老师的教学链接  本课讲解 指向基类的指针,为何只能访问来自基类成员?! #include <iostream> #include <string> using namespace std; //声明基类Student class Student { public: Student(int, string,float);//声明构造函数 void display( ); //声明输出函数 protected: //受保护成员,派生类可以访问 int num; string nam

C++语言基础 例程 析构函数

贺老师的教学链接  本课讲解 析构函数示例 #include<string> #include<iostream> using namespace std; class Student { public: Student(int n,string nam,char s) { num=n; name=nam; sex=s; cout<<"执行构造函数:"<<name<<" come."<<endl

C++语言基础 例程 虚基类及应用

贺老师的教学链接  本课讲解 虚基类应用举例 #include <iostream> #include <cstring> using namespace std; class Person { public: Person(char *nam,char s,int a) //构造函数 { strcpy(name,nam); sex=s; age=a; } protected: //保护成员 char name[20]; char sex; int age; }; class Te

C++语言基础 例程 案例:MyVector类的设计

贺老师的教学链接  本课讲解 //MyVector类的设计 #include <iostream> using namespace std; class MyVector //定义向量类 { public: MyVector(int m); //构造函数,共有m个元素的向量,元素值预置为0 MyVector(const MyVector &v); //复制构造函数 ~MyVector(); //析构函数:释放动态数组所占用的存储空间 friend istream &operat