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,int m,int s)
{
    hour=h;
    minute=m;
    sec=s;
}

void Time::show_time( )
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( )
{
    Time t1;
    t1.show_time();
    Time t2(8);
    t2.show_time();
    Time t3(8,30);
    t3.show_time();
    Time t4(18,56,48);
    t4.show_time();
    return 0;
}
时间: 2024-07-30 08:49:43

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

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++中构造函数的默认参数和构造函数的重载_C 语言

C++构造函数的默认参数 和普通函数一样,构造函数中参数的值既可以通过实参传递,也可以指定为某些默认值,即如果用户不指定实参值,编译系统就使形参取默认值. [例] #include <iostream> using namespace std; class Box { public : Box(int h=10,int w=10,int len=10); //在声明构造函数时指定默认参数 int volume( ); private : int height; int width; int l

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

贺老师的教学链接  本课讲解 默认构造函数(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; int main( ) { float grade; cout<<"enter grade:"; while(cin>>grade)//能从cin流读取数据 { if(grade>=85) cout<<grade<<" GOOD!"<<endl; if

C++语言基础 例程 函数重载

贺老师的教学链接 重载函数:同名同体,但接口不同 #include <iostream> using namespace std; int max(int a,int b,int c); //函数声明 double max(double a,double b,double c); long max(long a,long b,long c); int main( ) { int i1,i2,i3,i; cin>>i1>>i2>>i3; //输入3个整数 i=

C++语言基础 例程 二进制文件应用案例

贺老师的教学链接  本课讲解 系统升级第一步:转换现有数据格式(附:数据文件点击打开链接) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; typedef struct { int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total; } Stude

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

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

贺老师的教学链接  本课讲解 问题的由来 #include <iostream> using namespace std; class Point { public: Point( ) { } ~Point() { cout<<"executing Point destructor"<<endl; } }; class Circle:public Point { public: Circle( ) { } ~Circle( ) { cout<&