第二章

来源:互联网 发布:源氏伤害数据 编辑:程序博客网 时间:2024/05/09 15:54

2.

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


3.


using namespace std;
class Time
{
 public:
 void cin_Time();
 void cout_Time();
 private:
 int hour;
    int minute;
    int sec;
   
};
int main()
{
 Time t;
 t.cin_Time();
 t.cout_Time();
 return 0;
}
void Time::cin_Time()
{
    cin>>hour>>minute>>sec;
}
void Time::cout_Time()
{
 cout<<hour<<":"<<minute<<":"<<sec;
}

4.

//student.h
#include<string>
using namespace std;
class student
{
 public:
 void display();
 int num;
 string name;
 char sex;
 
};
//student.cpp
#include<iostream>
#include"student.h"
void student::display()
{
 cout<<"num:"<<num<<endl;
 cout<<"name:"<<name<<endl;
 cout<<"sex:"<<sex<<endl;
}

//main.cpp
#include <iostream>
#include"student.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main()
{
 student stud;
 cin>>stud.num>>stud.name>>stud.sex;
 stud.display();
 return 0;
}

5.

//array_max.h
#include<string>
using namespace std;
class array_max
{
 public:
 void set_value();
 void max_value();
 void show_value();
 private:
 int array[10];
 int max;
};
//array_max.cpp
#include<iostream>
#include"array_max.h"
using namespace std;
void array_max::set_value()
{
 int i;
 for(i=0;i<10;i++)
 {
  cin>>array[i];
 }
}
void array_max::max_value()
{
 int i;
 max=array[0];
 for(i=1;i<10;i++)
 {
  if(array[i]>max)max=array[i];
 }
}
void array_max::show_value()
{
 cout<<"max="<<max;
}
#include<iostream>
#include"array_max.h"
using namespace std;
int main()
{
 array_max arrmax;
 arrmax.set_value();
 arrmax.max_value();
 arrmax.show_value();
 return 0;
}


6..

#include <iostream>
#include"v_sum.h"
using namespace std;
int main()
{
 v_sum v1,v2,v3;
 int v_sum1,v_sum2,v_sum3;
 v1.get();
 v_sum1=v1.getv();
 v2.get();
 v_sum2=v2.getv();
 v3.get();
 v_sum3=v3.getv();
 cout<<"v1="<<v_sum1<<endl<<"v2="<<v_sum2<<endl<<"v3="<<v_sum3;
 return 0;
}
//v_sum.h
using namespace std;
class v_sum
{
 public:
 void get();
 int getv();
 private:
 int lengh;
 int width;
 int height;
};
//v_sum.cpp
#include<iostream>
#include"v_sum.h"
using namespace std;
void v_sum::get()
{
 cout<<"分别输入长宽高"<<endl;
 cin>>lengh>>width>>height;
 cout<<"lengh="<<lengh<<endl<<"width="<<width<<endl<<"height="<<height<<endl;
}
int  v_sum::getv()
{
 int v=0;
 v=lengh*width*height;
 return v;
}


0 0