北邮C++ 实验六
- 格式:doc
- 大小:195.00 KB
- 文档页数:17
实验六类与对象一、实验目的1.掌握面向对象程序设计的特点。
2.掌握类的使用、定义和实现.2. 熟悉面向对象的编程方法。
二、实验内容1.按要求完成下列各题(1)调试程序中的语法错误,使之输出下列结果:#include <iostream>using namespace std;clase Time{Private:int hour;int minute;int sec;Publice:Void set_time( );Void show_time( );}Void Time::set_time( );{ cin>>hour;Cin>>minute;Cin>>sec;}Void Time::show_time( );{Cout<<hour<<”:”<<minute<<”:”<<sec<<endl; }int main(){ Time t;T.set_time();T.show_time();}运行结果:输入:14 02 56输出:14:02:56要求:①添加语句注释。
②将程序中的数据成员改为公有、函数改成普通函数后,如何修改程序,并输出正确的结果。
Public:int hour;int minute;int sec;int main(){ set_time();show_time();return 0;}③修改程序,将类的声明部分定义成一个头文件,并上机调试。
Solution:1. #include<iostream>using namespace std;class Time{private:int hour;int minute;int sec;public:void set_time( );void show_time( );};(定义一个类)void Time::set_time( ){ cin>>hour;cin>>minute;cin>>sec;}void Time::show_time( ){cout<<hour<<":"<<minute<<":"<<sec<<endl; }(类外定义函数)int main(){ Time t;t.set_time();t.show_time();(主函数中调用类内函数)}2.solution:#include"stdafx.h"#include<iostream>using namespace std;class Time{public:int hour;int minute;int sec;};Time a;void set_time( int x,int y,int z){a.hour=x,a.minute=y,a.sec=z;}void show_time(){cout<<a.hour<<":"<<a.minute<<":"<<a.sec<<endl; }int main(){set_time(12,9,8 );show_time( );}(2)上机调试下列程序,使之输出下列结果:This is a test.Length:15I like VC++Freeing pFreeing p要求:①添加语句注释。
②简答:在程序下面写出构造函数和析构函数在程序中能否直接调用;如果程序中未定义构造函数,编译系统是否会自动产生一个构造函数。
程序:#include <string.h>#include<stdio.h>class StrType{public:StrType(char *ptr);~StrType();show();private:char *p;int len;};StrType::StrType(char *ptr){len=strlen(ptr);*p=new char();if(!p){cout<<("Allocation error\n");}strcpy(p,ptr);}StrType::~StrType(){cout>>"Freeing p">>endl;delete p;}void StrType::show(){cout<<*p<<endl;cout<<"length:"<<len<<endl;}void main(){ char ch1[15];cin>>ch1;StrType s1(ch1);Char ch2[12];StrType s2(ch2);s1.show();s2.show();}Solution:#include<iostream>#include<string.h>#include<stdio.h>using namespace std;class StrType{public:StrType(char *ptr);~StrType();void show();private:char *p;int len;};StrType::StrType(char *ptr) {len=strlen(ptr);p=new char[len];if(!p){cout<<("Allocation error\n"); }strcpy(p,ptr);}StrType::~StrType(){cout<<"Freeing p"<<endl; delete p;}void StrType::show(){cout<<"Length:"<<len<<endl; }int main(){ char ch1[15];gets(ch1);StrType s1(ch1);s1.show();char ch2[12];gets(ch2);StrType s2(ch2);//system("pause");return 0;}2.编一程序,用类实现求5!的值(要求使用构造函数、析造函数)。
Solution:#include<iostream>using namespace std;class number{private:long num;public:number(long m=4){num=m;long sum=1;for(long i=1;i<=num;i++){sum=sum*i;}cout<<sum<<endl;}~number(){cout<<"析构对象"<<endl;}};void main(){number a;number b(5);}3.定义一个Employee类,其中包括表示姓名、性别、年龄、城市和邮编等属性,包括chage_name()和display()等函数;display()显示姓名、性别、城市和邮编等属性,函数change_name()改变对象的姓名、年龄、城市属性,实现并测试这个类。
Solution:#include<iostream>using namespace std;class Employee {private:char *name,*address,*city,*postCode;public:Employee(char *_name,char *_address,char *_city,char *_postCode){name = _name;address = _address;city = _city;postCode = _postCode;}void change_name(char *_name){name = _name;}void display(){cout << "name : " << name << endl;cout << "address : " << address << endl; cout << "city : " << city << endl;cout << "postcode : " << postCode << endl; }};int main(int argc,char *argv[]){Employee *e = new Employee("zhangsan","chang d d","beijing","100056");e->display();e->change_name("lisi");e->display();delete e;}4.用类结构的形式编写一个程序,使其可实现输入5个学生姓名、性别和年龄,并按年龄从大到小进行排序输出。
Solution:#include"stdafx.h"#include<iostream>#include<string>using namespace std;#define MAX 5class student{private:string Name;string Sex;public:string Age;student(string name="\0",string age="\0",string sex="\0"){Name=name;Age=age;Sex=sex;}void input();void display();};void student::input(){cout<<"请输入姓名"<<endl;cin>>Name;cout<<"请输入年龄"<<endl;cin>>Age;cout<<"请输入性别"<<endl;cin>>Sex;void student::display(){cout<<"姓名"<<Name<<endl;cout<<"年龄"<<Age<<endl;cout<<"性别"<<Sex<<endl;}void main(){cout<<"请输入个学生信息"<<endl;student str[MAX];for(int i=0;i<5;i++)str[i].input();cout<<"---------------------------------------------------------------"<<endl;cout<<"以下是输出信息"<<endl;for(int j=0;j<4;j++){for(int i=0;i<4;i++){if(str[i].Age<str[i+1].Age)continue;else{student x;x=str[i];str[i]=str[i+1];str[i+1]=x;}}}for(int j=0;j<5;j++){str[j].display();}}三、实验报告写出程序清单和运行结果。