C++作业

来源:互联网 发布:for在c语言中的作用 编辑:程序博客网 时间:2024/05/01 06:15
1、

#include <iostream>

using namespace std;

class Time

 {

public:

         voidset_time();

         voidshow_time();

private:                    //成员改为公用的

   int hour;

   int minute;

   int sec;

 

  };

 

void Time::set_time()          //在main函数之前定义

 {

 cin>>hour;

 cin>>minute;

 cin>>sec;

 }

 

void Time::show_time()         //在main函数之前定义

 {

 cout<<hour<<":"<<minute<<":"<<sec<<endl;

 }

 

int main()

{Time t1;

t1.set_time();

 t1.show_time();

 return 0;

}


2、

#include <iostream>

using namespace std;

class Time

 {public:

   void set_time(void)

    {cin>>hour;

     cin>>minute;

     cin>>sec;

    }

   void show_time(void)

    {cout<<hour<<":"<<minute<<":"<<sec<<endl;}

 

  private: int hour;

    int minute;

    int sec;

  };

 

Time t;

int main()

 {

 t.set_time();

 t.show_time();

 return 0;

 }


3、

#include <iostream>

using namespace std;

class Time

 {public:

   void set_time(void);

   void show_time(void);

  private:

   int hour;

   int minute;

   int sec;

  };

 

void Time::set_time(void)

 {cin>>hour;

  cin>>minute;

  cin>>sec;

  }

 

void Time::show_time(void)

{cout<<hour<<":"<<minute<<":"<<sec<<endl;}

 

Time t;

int main()

{ t.set_time();

 t.show_time();

 return 0;


4、

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


6、

#include<iostream>

using namespacestd;

class  Cuboid

{public:

         void get_value();

         void volume();

         void display();

private:

          float length,width,height,vol;

};

void Cuboid::get_value()

{

 cin>>length>>width>>height;

};

void Cuboid::volume()

{

 vol=length*width*height;

}

void Cuboid::display()

{

 cout<<vol<<endl;

}

int main()

{

 Cuboid c1,c2,c3;

 c1.get_value();

 c1.volume();

 cout<<"The volume of c1 is:";

 c1.display();

 c2.get_value();

 c2.volume();

 cout<<"The volume of c2 is:";

 c2.display();

 c3.get_value();

 c3.volume();

 cout<<"The volume of c3 is:";

 c3.display();

 return 0;

}


0 0
原创粉丝点击