国开网[00007]《C++语言程序设计》形考作业3(四选一实验上传“实验结果截图”)
形考作业3(四选一实验上传“实验结果截图”)
实验9-实验12任选一作业,并将“实验结果截图”以附件形式上传到这里
参考答案:
上机实验9:操作符重载的应用
一、实验内容
下面程序定义了复数类Complex的3个对象:Complex u(10, 20), v(5,-4), s;
在程序中通过s = u + v;来计算u和v的和,并将结果存入s中,填补缺失的语句并调试出正确运行结果。
#include <iostream>
using namespace std;
class Complex
{private:
double real, imag; //real、imag分别保存复数的实部、虚部
public:
Complex() { real = 0; imag = 0;}
Complex(double r, double i) { real = r; imag = i; }
void display()
{ cout<< "(" << real;
if (imag >0) cout<< "+" << imag << "i)";
else if (imag <0) cout<< imag << "i)";
}
// 通过成员函数重载运算符“+”
};
void main()
{ Complex u(10, 20), v(5,-4), s;
s = u + v; // 使用重载的运算符“+”
u.display(); cout<<" + ";
v.display(); cout<<" = ";
s.display(); cout<
}二、实验要求
在Complex类中编写重载运算符“+”的函数,保证语句的用法正确并显示复数运算结果。
三、实验结果截图
(请将实际上机运行的结果截图并上传)
上机实验10:类继承的应用
一、实验内容
已知一个人的类,具有姓名、性别、身高等属性,其成员函数能够打印输出该类的信息;实现一个学生的派生类,成员变量中增加学号,并且能够打印学生的个人信息。
//头文件People.h
#pragma once
class People
{
public:
People(char* name,char* sex,float height);
~People(void);
private:
char name[10];
char sex[10];
float height;
public:
void showInfo();
};
//程序文件People.cpp
#include "StdAfx.h"
#include "People.h"
#include <iostream>
using namespace std;
People::People(char* name,char* sex,float height)
{
strcpy_s(this->name,name);
strcpy_s(this->sex,sex);
this->height=height;
}
People::~People(void)
{
}
void People::showInfo()
{
cout<<"姓名:"<
}//请补充派生类头文件CStudent.h
//请补充派生类程序文件CStudent.cpp”
//请补充主程序文件
二、实验要求
(1)补充程序中缺失的语句;
(2)调试程序,使其能够输出下图的运行结果。
三、实验结果截图
(请将实际上机运行的结果截图并上传)
上机实验11:类的多态性应用
一、实验内容
已知人类是学生类和教师类的基类,调试下面程序使之运行后输出下图结果?改写程序在主函数中使用指针数组的方式使之输出相同的运行结果。
//头文件People.h
#pragma once
class CPeople
{
public:
CPeople(char* name);
~CPeople(void);
private:
char name[10];
public:
void display();
char* getName();
};
//程序文件People.cpp
#include "StdAfx.h"
#include "People.h"
#include <iostream>
using namespace std;
CPeople::CPeople(char* name)
{
strcpy(this->name,name);
}
CPeople::~CPeople(void)
{
}
void CPeople::display()
{
cout<<"我是人,名字为:"<
}
char* CPeople::getName()
{
return name;
}
//头文件Student.h
#pragma once
#include "people.h"
class CStudent :
public CPeople
{
public:
CStudent(char* name);
~CStudent(void);
void display();
};
//程序文件Student.cpp
#include "StdAfx.h"
#include "Student.h"
#include <iostream>
using namespace std;
CStudent::CStudent(char* name):CPeople(name)
{
}
CStudent::~CStudent(void)
{
}
void CStudent::display()
{
cout<<"我是学生,姓名为:"<
}
//头文件Teacher.h
#pragma once
#include "People.h"
class CTeacher :
public CPeople
{
public:
CTeacher(char* name);
~CTeacher(void);
void display();
};
//程序文件Teacher.cpp
#include "StdAfx.h"
#include "Teacher.h"
#include <iostream>
using namespace std;
CTeacher::CTeacher(char* name):CPeople(name)
{
}
CTeacher::~CTeacher(void)
{
}
void CTeacher::display()
{
cout<<"我是老师,姓名为:"<
}
//主程序文件main.cpp
#include "stdafx.h"
#include "iostream"
#include "Student.h"
#include "Teacher.h"
int _tmain(int argc, _TCHAR* argv[])
{
CStudent st1("zhangsan");
CTeacher st2("lisi");
CPeople* pe;
pe=&st1;
pe->display();
pe=&st2;
pe->display();
return 0;
}二、实验要求
(1)调试、补充完整程序中缺失的语句;
(2)改写、调试程序,使其能够输出图示运行结果。
三、实验结果截图
(请将实际上机运行的结果截图并上传)
上机实验12:文件流的应用
一、实验内容
下面的程序将5个Student记录按无格式方式写入一文件中,然后再读取出来。
//头文件Student.h
#include <iostream>
using namespace std;
class Student{ //学生类
char ID[10]; //学号
char name[20]; //姓名
char sex[3]; //性别
public:
Student(){}
Student(const char *id,const char *n,const char *s){
strcpy_s(ID,id);
strcpy_s(name,n);
strcpy_s(sex,s);
}
friend ostream& operator << (ostream &os, Student s);
};
//将数组st中的num个学生记录写入filename所指定的文件中保存
void writeToFile(Student st[],int num,const char *filename);
//将filename所指定的文件中的num个学生记录读入数组st中
void readFromFile(Student st[],int num,const char *filename);
//程序文件Student.cpp
#include "Student.h"
#include <iomanip>
#include <fstream>
ostream& operator << (ostream &os, Student s){
os<
return os;
}
void writeToFile(Student st[],int num,const char *filename){
//请补充缺失的语句
Os.write((char *)&st[2],sizeof(Student));
.
.
cout<<"已写入的记录:"<
for(int i=0;i<5;i++) cout<
}
void readFromFile(Student st[],int num,const char *filename){
//请补充缺失的语句
.
.
cout<<"已读取的记录:"<
for(int i=0;i<5;i++) cout<
}
//主程序文件main.cpp
#include "Student.h"
Student s1[]={
Student("111111","张三","男"),
Student("222222","李四","女"),
Student("333333","王五","男"),
Student("444444","赵六","男"),
Student("555555","吴七","女")
};
writeToFile(s1,5,"C:\\STUDENT.DAT");
Student s2[5];
readFromFile(s2,5,"C:\\STUDENT.DAT");
return 0;
}
程序的输出应该是:
已写入的记录:
111111 张三 男
222222 李四 女
333333 王五 男
444444 赵六 男
555555 吴七 女
已读取的记录:
111111 张三 男
222222 李四 女
333333 王五 男
444444 赵六 男
555555 吴七 女二、实验要求
(1)补充完整程序中缺失的语句;
(2)调试程序,使其能够正常运行。
三、实验结果截图
(请将实际上机运行的结果截图并上传)




![国开学习网[02557]《现代汉语专题》形考任务3答案](http://guokaixuexi.com/zb_users/upload/2023/12/202312151702572538299927.png)