c++ 类的学习2
时间:2011-06-08 来源:hut-u~p
#include<iostream> #include<stdlib.h> #include<string> using namespace std; class Time { public: void set_time(); //公有成员函数,声明函数 void show_time();//公有成员函数,声明函数 private: int hour; int minute; int sec; }; int main( ) { Time t1,t2; t1.set_time(); t1.show_time(); t2.set_time(); t2.show_time(); system("pause"); return 0; } void Time::set_time( ) { cin>>hour>>minute>>sec; } void Time::show_time() { cout<<hour<<":"<<minute<<":"<<sec<<endl; }
#include<iostream> #include<string> #include<stdlib.h> using namespace std; class student { public: void display() { cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } void set( ) { cin>>num; cin>>name; getchar(); cin>>sex; } private: int num; char name[20]; char sex; }stud1,stud2; int main() { stud1.set(); stud1.display(); system("pause"); return 0; }
相关阅读 更多 +