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;
}
时间: 2024-07-30 08:49:22

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

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> 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++语言基础 例程 构造函数

贺老师的教学链接  本课讲解 用构造函数初始化对象 #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; ci

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++语言基础 例程 有默认参数的函数

贺老师的教学链接 形参/实参.声明/调用/定义 #include <iostream> using namespace std; int max(int a, int b, int c=0);//仅声明时设默认 int main( ) { int a,b,c; cin>>a>>b>>c; cout<<"max(a,b,c)="<<max(a,b,c)<<endl; cout<<"m

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++语言基础 例程 标准输出流

贺老师的教学链接  本课讲解 cerr流对象使用:解方程ax^2+bx+c=0 //解一元二次方程ax^2+bx+c=0:从键盘输入a,b,c的值,求x1和x2.如果a=0或b2-4ac<0,输出出错信息. #include<iostream> #include <cmath> #include<iomanip> using namespace std; int main( ) { float a,b,c,delta; cout<<"plea