struct Student Student class Student int num char

  • Slides: 37
Download presentation

struct 可以将上面类的声明改为: Student //声明了一个名为Student的结构体类型 { class Student //声明类类型 int num; {char name[20]; char sex;

struct 可以将上面类的声明改为: Student //声明了一个名为Student的结构体类型 { class Student //声明类类型 int num; {char name[20]; char sex; private: //声明以下部分为私有的 }; int num; Student stud 1, stud 2; //定义了两个结构体变量stud 1和stud 2 char name[20]; class Student //以class开头 char sex; { public: //声明以下部分为公有的 int num; void display( ) char name[20]; char sex //以上3行是数据成员 { ; void display() //这是成员函数 cout <<"num: "<< num << endl; { cout <<"name: "<< cout <<"num: "<< num << endl ; name << endl; cout <<"name: "<< name << endl; cout <<"sex: "<< sex << endl; cout << "sex: "<< sex <<endl; //以上4行是函数中的操作语句 } } }; }; 5 Student //定义了两个类对象stud 1和stud 2 Student stud 1, stud 2; //定义了两个类对象stud 1和stud 2

void main() <iostream. h> { #include Complex a; class Complex a. init(2, 3); {private:

void main() <iostream. h> { #include Complex a; class Complex a. init(2, 3); {private: cout<<a. real()<<"+“<<a. image()<<"i"<<endl; double r; //错误, a. r=6; double i; //错误 a. i=3. 2; } public: void init(double rr, double ii) { r=rr; i=ii; } double real() { return r; } double image() { return i; } }; 7

u 内联成员函数 #include<iostream. h> class point { private : int x, y; public: void

u 内联成员函数 #include<iostream. h> class point { private : int x, y; public: void setpoint(int, int); int getx(); int gety(); }; inline void point: : setpoint(int a, int b) { x=a; y=b; } inline int point: : getx(){return x; } inline int point: : gety(){return y; } main () { } 13 #include<iostream. h> class point { private : int x, y; public: void setpoint(int a, int b) { x=a; y=b; } int getx(){return x; } int gety(){return y; } }; main () { int x=9, y=20; point p 1; int x=9, y=20; p 1. setpoint(x, y); point p 1; cout<<"point. x="<<p 1. getx()<<endl; p 1. setpoint(x, y); cout<<"point. y="<<p 1. gety()<<endl; cout<<"point. x="<<p 1. getx()<<endl; cout<<"point. y="<<p 1. gety()<<endl; }

8. 1类和对象—应用举例 19 【例8 -1】 一个简单的例子 #include int main( )<iostream> { using namespace std;

8. 1类和对象—应用举例 19 【例8 -1】 一个简单的例子 #include int main( )<iostream> { using namespace std; class. Timet 1; //定义Time类 //定义t 1为Time类对象 { cin >> t 1. hour; //输入设定的时间 public: //数据成员为公有的 cin >> t 1. minute; int cinhour; >> t 1. sec; int minute; cout << t 1. hour<< “: ” << t 1. minute; int sec; cout <<": "<< t 1. sec << endl; }; return 0; }

int main( ) 例8 -2:利用函数实现相同功能 //将主函数以后改为以下内容 voidsettime(Time&t) t, int hour, int minute, intsec) void

int main( ) 例8 -2:利用函数实现相同功能 //将主函数以后改为以下内容 voidsettime(Time&t) t, int hour, int minute, intsec) void //定义函数settime, 形参t是引用变 {int#include ) <iostream> 量 {voidmain( settime(Time&); //函数声明 using namespace std; {//函数声明 {void showtime(Time&); t. hour=hour; //函数声明 class Time void settime(Time&, int hour=0, int minute=0, int sec=0 cin>>t. hour; //输入设定的时间 t. minute=minute; Time t 1; //定义t 1为Time类对象 cin>>t. minute; { showtime(Time&); void //函数声明 settime(t 1); //调用函数settime, 向对象t 1中的数据成员输入数 t. sec=sec; cin>>t. sec; Time t 1; 据 } public: }showtime(t 1); //调用函数showtime, 输出对象t 1中的数据 int hour; settime(t 1, 12, 23, 34); //通过实参传递时、分、秒的值 void showtime(Time& t) Time t 2; int minute; //定义t 2为Time类对象 void showtime(Time& t) //定义函数showtime, 形参t是引 showtime(t 1); { settime(t 2); //调用函数settime, 向对象t 2中的数据成员输入数 用变量 int sec; Timecout<<t. hour<< t 2; ": "<<t. minute<< ": "<<t. sec<<endl 据 { }; settime(t 2); //使用默认的时、分、秒的值 }cout<<t. hour<< showtime(t 2); //调用函数showtime, 输出对象t 2中的数据 ": "<<t. minute<< ": "<<t. sec<<endl; //输 showtime(t 2); return 0; 出对象中的数据 return 0; } } 20 }

21 例8 -3:将上述程序改用含成员函数的类来处理。 void int main( Time∷settime( ) ) //在类外定义函数settime { #include <iostream> using

21 例8 -3:将上述程序改用含成员函数的类来处理。 void int main( Time∷settime( ) ) //在类外定义函数settime { #include <iostream> using std; Time cin>>hour; t 1; namespace //定义对象t 1 class Time); //调用对象t 1的成员函数settime, 向t 1的数据成员输入数据 t 1. settime( cin>>minute; { t 1. showtime( cin>>sec; ); //调用对象t 1的成员函数showtime, 输出t 1的数据成员的值 public: }Time t 2; //定义对象t 2 void settime( ); //公有成员函数 t 2. settime( void Time∷showtime( ); //调用对象t 2的成员函数settime, 向t 2的数据成员输入数据 ) //在类外定义函数showtime void showtime( ); //公有成员函数 {t 2. showtime( ); //调用对象t 2的成员函数showtime, 输出t 2的数据成员的值 private: return cout<<hour<< 0; //数据成员为私有 ": "<<minute<< ": "<<sec<<endl; int hour; } int minute; int sec; };

int【例8 -4】 main( ) 找出一个整型数组中元素的最大值。 void Array_max∷set_value( ) //成员函数定义, 向数组元素输入数值 { #include int i;

int【例8 -4】 main( ) 找出一个整型数组中元素的最大值。 void Array_max∷set_value( ) //成员函数定义, 向数组元素输入数值 { #include int i; <iostream> fornamespace (i=0; i<10; i++) Array_max arrmax; //定义对象arrmax using std; cin>>array[i]; arrmax. set_value( ); //声明类 //调用arrmax的函数set_value, 向数组元素输入数值 class Array_max } arrmax. max_value( ); //调用arrmax的函数max_value, 找出数组元素中的最大值 { Array_max∷max_value( void ) //成员函数定义, 找数组元素中的最大值 arrmax. show_value( ); //调用arrmax的函数show_value, 输出数组元素中的最大值 { public: int i; //以下3行为成员函数原型声明 returnvoid 0; set_value( ); //对数组元素设置值 max=array[0]; for (i=1; i<10; i++) } void max_value( ); //找出数组中的最大元素 if(array[i]>max)); //输出最大值 void show_value( max=array[i]; private: } int array[10]; //整型数组 void Array_max∷show_value( ) //成员函数定义, 输出最大值 int max; //max用来存放最大值 { cout<< "max="<<max; } }; 22

void main() 【例】 Clock类及其对象的完整例程。 { #include<iostream> Clock *pa, *pb, a. Clock, b. Clock; #include<string>a.

void main() 【例】 Clock类及其对象的完整例程。 { #include<iostream> Clock *pa, *pb, a. Clock, b. Clock; #include<string>a. Clock. set. Minute(12); using namespace std; a. Clock. set. Hour(16); class Clock a. Clock. set. Second(27); b. Clock=a. Clock; { public: pa=new void set. Hour(int h) { Clock; hour=h; } pa->set. Hour(10); void set. Minute(int m) { minute=m; } pa->set. Minute(23); void set. Second(int s) { second=s; } pa->set. Second(34); void disp. Time() pb=pa; pa->disp. Time(); { cout<<"Now is: ”<<hour<<": "<<minute<<": "<<second<<endl; } pb->disp. Time(); private: a. Clock. disp. Time(); int hour, minute, second; b. Clock. disp. Time(); }; } 23