avatar

c/c++第十次作业

GitHub

https://github.com/Joe1sn/C-Cpp-shitty-code/tree/master/10

目录结构

cb7b6e61067c1b9a2e721bd3f61b0776.png

Makefile

Answer_n: main.cpp head.cpp
g++ -std=c++11 -o Answer_n main.cpp head.cpp

objects: main.o head.o
.PHONY : clean
clean :
-rm $(objects)

10-2

2.cpp

#include <iostream>
#include <cstring>
using namespace std;

class Person
{
private:
char Name[20];
char Sex[4];
unsigned int Age;
public:
Person(char *name, unsigned int age, char *sex);
void PrintAll();
};
Person::Person(char *name, unsigned int age, char *sex)
{
strcpy(Name,name);
Age=age;
strcpy(Sex,sex);
}
void Person::PrintAll()
{
cout<<"姓名> "<<Name<<endl;
cout<<"年龄> "<<Age<<endl;
cout<<"性别> "<<Sex<<endl;
}

class Teacher: private Person
{
private:
char Major[30];
char ClassTought[30];
char JobTitile[16];
public:
Teacher(char *name, unsigned int age, char *sex,
char *major, char *classtought, char *jobtitile);
void PrintAll();
};
Teacher::Teacher(char *name, unsigned int age, char *sex,
char *major, char *classtought, char *jobtitile)
:Person(name, age, sex)
{
strcpy(Major,major);
strcpy(ClassTought,classtought);
strcpy(JobTitile,jobtitile);
}
void Teacher::PrintAll()
{
Person::PrintAll();
cout<<"专业> "<<Major<<endl;
cout<<"主讲课程> "<<ClassTought<<endl;
cout<<"职称> "<<JobTitile<<endl;
}


int main()
{
Person a1("joe1sn",27,"男");
a1.PrintAll();
cout<<endl;
Teacher a2("joe2sn",45,"女","计算机与软件工程","C/C++程序设计基础","讲师");
a2.PrintAll();
return 0;
}

运行截图

18b239f332d2de2cced764ae179c3a11.png

10-3

head.h

#ifndef _GRAGUATE_H
#define _GRAGUATE_H

//定义一个基类
class Person
{
protected:
char Name[20];
char Sex[4];
unsigned int Age;
public:
Person(char *name, unsigned int age, char *sex);
void PrintAll();
};

//由基类派生一个教师类
class Teacher: protected Person
{
private:
char Major[20];
char ClassTought[30];
char JobTitile[16];
protected:
void PrintTeacher();
public:
Teacher(char *name, unsigned int age, char *sex,
char *major, char *classtought, char *jobtitile);
void PrintAll();
};

//由基类派生出一个学生类
class Student: protected Person
{
private:
unsigned int ID;
unsigned short int Score;
protected:
void PrintStudent();
public:
Student(char *name, unsigned int age, char *sex,
unsigned int id, unsigned short int score);
void PrintAll();
};

//研究生类继承教师类和学生类
class Graduate: protected Teacher, protected Student
{
protected:
void PrintGraduate();
public:
Graduate(char *name, unsigned int age, char *sex,//基类成员
char *major, char *classtought, char *jobtitile,//教师类成员
unsigned int id, unsigned short int score);//学生类成员
void PrintAll();
};
#endif

head.cpp

#include <iostream>
#include <cstring>
#include "head.h"
using std::cout;
using std::endl;

//实现基类Person的函数功能
Person::Person(char *name, unsigned int age, char *sex)
{
strcpy(Name,name);
Age=age;
strcpy(Sex,sex);
}
void Person::PrintAll()
{
cout<<"姓名> "<<Name<<endl;
cout<<"年龄> "<<Age<<endl;
cout<<"性别> "<<Sex<<endl;
}

//实现教师类的函数功能
Teacher::Teacher(char *name, unsigned int age, char *sex,
char *major, char *classtought, char *jobtitile)
:Person(name, age, sex)
{
strcpy(Major,major);
strcpy(ClassTought,classtought);
strcpy(JobTitile,jobtitile);
}
void Teacher::PrintTeacher()
{
cout<<"专业> "<<Major<<endl;
cout<<"主讲课程> "<<ClassTought<<endl;
cout<<"职称> "<<JobTitile<<endl;
}
void Teacher::PrintAll()
{
Person::PrintAll();
Teacher::PrintTeacher();
}

//实现学生类的函数功能
Student::Student(char *name, unsigned int age, char *sex,
unsigned int id, unsigned short int score)
:Person(name, age, sex)
{ ID=id; Score=score; };
void Student::PrintStudent()
{
cout<<"学号> "<<ID<<endl;
cout<<"考研成绩> "<<Score<<endl;
}
void Student::PrintAll()
{
Person::PrintAll();
Student::PrintStudent();
}

//实现研究生的函数功能
Graduate::Graduate(char *name, unsigned int age, char *sex,//基类成员
char *major, char *classtought, char *jobtitile,//教师类成员
unsigned int id, unsigned short int score)//学生类成员
:Teacher(name, age, sex, major, classtought, jobtitile),
Student(name, age, sex, id, score){};
void Graduate::PrintAll()
{
Teacher::PrintAll();
Student::PrintStudent();
}

main.cpp

#include <iostream>
#include <cstring>
#include "head.h"
using std::cout;
using std::endl;

int main()
{
Graduate a1("张三",25,"男",
"计算机科学","C/C++程序设计","讲师",
10001,200);
a1.PrintAll();
return 0;
}

运行截图

c9658f67e8b1cf82f3a2219e023ee42b.png

10-4

head.h

#ifndef _HEAD_H
#define _HEAD_H

//定义一个shape的基类
class Shape
{
protected:
int CenterX, CenterY;
public:
Shape(int centerx, int centery);
void PrintAll();
};

//定义一个circle类
class Circle: protected Shape
{
private:
double R, S;
public:
Circle(int centerx, int centery, double r);
void PrintAll();
};

//定义一个square类
class Square: protected Shape
{
private:
int EgdeX, EgdeY;
int S;
public:
Square(int centerx, int centery,
int egdex, int egdey);
void PrintAll();
};
#endif

head.cpp

#include <iostream>
#include "head.h"
using std::cout;
using std::endl;
const double PI = 3.14159;

//实现shape类的函数功能
Shape::Shape(int centerx, int centery)
{ CenterX=centerx; CenterY=centery; };
void Shape::PrintAll()
{
cout<<"中心点坐标 ("<<CenterX<<","<<CenterY<<")\n";
}

//实现circle类的函数功能
Circle::Circle(int centerx, int centery, double r)
:Shape(centerx, centery)
{
R=r;
S=PI*R*R;
}
void Circle::PrintAll()
{
Shape::PrintAll();
cout<<"半径> "<<R<<endl;
cout<<"面积> "<<S<<endl;
}

//实现square类的函数功能
Square::Square(int centerx, int centery,
int egdex, int egdey)
:Shape(centerx, centery)
{
EgdeX=egdex; EgdeY=egdey;
S = (EgdeX-CenterX)*(EgdeY-CenterY)*4;
};
void Square::PrintAll()
{
Shape::PrintAll();
cout<<"顶点坐标> ("<<EgdeX<<","<<EgdeY<<")\n";
cout<<"面积> "<<S<<endl;
}

main.cpp

#include <iostream>
#include "head.h"
using std::cout;
using std::endl;
int main()
{
Shape A(1,1);
A.PrintAll();
cout<<endl;
Circle B(1,1,2.0);
B.PrintAll();
cout<<endl;
Square C(1,1,4,4);
C.PrintAll();
return 0;
}

运行截图

35525293d40b51aa0d2066000615d7d1.png

10-5

head.h

#ifndef _HEAD_H
#define _HEAD_H

//定义基类:哺乳类
class Mammal
{
protected:
char GenSerial[50];
public:
Mammal(char *genserial);
void PrintAll();
};

//----------------开始派生----------------
//定义 哺乳类派生:人类
class Person: protected Mammal
{
private:
char Name[20];
public:
Person(char *genserial, char *name);
void Speak();
};

//定义 哺乳类派生:狗类
class Dog: protected Mammal
{
private:
char Name[20];
public:
Dog(char *genserial, char *name);
void Speak();
};

//定义 哺乳类派生:人类
class Cat: protected Mammal
{
private:
char Name[20];
public:
Cat(char *genserial, char *name);
void Speak();
};

#endif

head.cpp

#include <iostream>
#include <cstring>
#include "head.h"
using std::cout;
using std::endl;
//实现哺乳类的函数
Mammal::Mammal(char *genserial)
{ strcpy(GenSerial,genserial); };
void Mammal::PrintAll()
{
cout<<"基因序列 >\n";
cout<<GenSerial<<endl;
}

//实现人类的函数
Person::Person(char *genserial, char *name)
:Mammal(genserial)
{ strcpy(Name,name); }
void Person::Speak()
{
Mammal::PrintAll();
cout<<"你好!\n";
};

//实现狗类的函数
Dog::Dog(char *genserial, char *name)
:Mammal(genserial)
{ strcpy(Name,name); };
void Dog::Speak()
{
Mammal::PrintAll();
cout<<"汪~\n";
};

//实现猫类函数
Cat::Cat(char *genserial, char *name)
:Mammal(genserial)
{ strcpy(Name,name); };
void Cat::Speak()
{
Mammal::PrintAll();
cout<<"喵~~~\n";
};

main.cpp

#include <iostream>
#include <cstring>
#include "head.h"
using std::cout;
using std::endl;
int main()
{
Mammal root("1010010110010110100");
root.PrintAll();
cout<<endl;
Person hunman("0101010101010","hunman");
hunman.Speak();
cout<<endl;
Dog dogge("1010010101010","dogge");
dogge.Speak();
cout<<endl;
Cat catty("0110010101100101","catty");
catty.Speak();
return 0;
}

运行截图

(公益图床无法过审。。。。这张的用的香港的图床)

运行截图.png

10-6

类关系

10-6.png

head.h

#ifndef _GRAGUATE_H
#define _GRAGUATE_H

//定义一个基类
class Person
{
protected:
char Name[20];
char ID[10];
public:
Person(char *name, char *id);
void PrintAll();
};

//由基类派生一个教师类
class Teacher: protected Person
{
private:
char Department[20];
char JobTitile[16];
protected:
void PrintTeacher();
public:
Teacher(char *name, char *id, char *department, char *jobtitile);
void PrintAll();
};

//由基类派生出一个学生类
class Student: protected Person
{
private:
char ClassNumber[10];
unsigned short int Score;
protected:
void PrintStudent();
public:
Student(char *name, char *id,char *classnumber, unsigned short int score);
void PrintAll();
};
#endif

head.cpp

#include <iostream>
#include <cstring>
#include "head.h"
using std::cout;
using std::endl;

//实现基类Person的函数功能
Person::Person(char *name,char *id)
{
strcpy(Name,name);
strcpy(ID,id);
}
void Person::PrintAll()
{
cout<<"姓名> "<<Name<<endl;
cout<<"编号> "<<ID<<endl;
}

//实现教师类的函数功能
Teacher::Teacher(char *name, char *id,
char *department, char *jobtitile)
:Person(name, id)
{
strcpy(Department,department);
strcpy(JobTitile,jobtitile);
}
void Teacher::PrintTeacher()
{
cout<<"单位> "<<Department<<endl;
cout<<"职称> "<<JobTitile<<endl;
}
void Teacher::PrintAll()
{
Person::PrintAll();
Teacher::PrintTeacher();
}

//实现学生类的函数功能
Student::Student(char *name, char *id,
char *classnumber, unsigned short int score)
:Person(name, id)
{
strcpy(ClassNumber,classnumber);
Score=score;
};
void Student::PrintStudent()
{
cout<<"学号> "<<ID<<endl;
cout<<"班号> "<<ClassNumber<<endl;
cout<<"成绩> "<<Score<<endl;
}
void Student::PrintAll()
{
Person::PrintAll();
Student::PrintStudent();
}

main.cpp

#include <iostream>
#include <cstring>
#include "head.h"
using std::cout;
using std::endl;

int main()
{
Teacher A("张三","711190","计算机与软件工程学院","讲师");
A.PrintAll();
cout<<endl;
Student B("李四","312233","09060102",345);
B.PrintAll();
return 0;
}

运行截图

91899baa772a789006e8375823fd9ab9.png

10-7

head.h

#ifndef _HEAD_H
#define _HEAD_H

//建立基类building
class Building
{
private:
unsigned int Layer;
unsigned int Room;
unsigned int Area;
public:
Building(unsigned int layer, unsigned int room, unsigned int area);
void PrintAll();
};

//由 building 类派生出 house 类
class House: private Building
{
private:
unsigned int BedRoom;
unsigned int Cell;
protected:
void PrintHouse();
public:
House(unsigned int layer, unsigned int room, unsigned int area,
unsigned int BedRoom, unsigned int Cell);
void PrintAll();
};

//由 building 类派生出 office 类
class Office: private Building
{
private:
unsigned int File;
unsigned long Phone;
protected:
void PrintOffice();
public:
Office(unsigned int layer, unsigned int room, unsigned int area,
unsigned long file, unsigned int phone);
void PrintAll();
};
#endif

head.cpp

#include <iostream>
#include <cstring>
#include "head.h"
using std::cout;
using std::endl;

//实现基类Person的函数功能
Person::Person(char *name,char *id)
{
strcpy(Name,name);
strcpy(ID,id);
}
void Person::PrintAll()
{
cout<<"姓名> "<<Name<<endl;
cout<<"编号> "<<ID<<endl;
}

//实现教师类的函数功能
Teacher::Teacher(char *name, char *id,
char *department, char *jobtitile)
:Person(name, id)
{
strcpy(Department,department);
strcpy(JobTitile,jobtitile);
}
void Teacher::PrintTeacher()
{
cout<<"单位> "<<Department<<endl;
cout<<"职称> "<<JobTitile<<endl;
}
void Teacher::PrintAll()
{
Person::PrintAll();
Teacher::PrintTeacher();
}

//实现学生类的函数功能
Student::Student(char *name, char *id,
char *classnumber, unsigned short int score)
:Person(name, id)
{
strcpy(ClassNumber,classnumber);
Score=score;
};
void Student::PrintStudent()
{
cout<<"学号> "<<ID<<endl;
cout<<"班号> "<<ClassNumber<<endl;
cout<<"成绩> "<<Score<<endl;
}
void Student::PrintAll()
{
Person::PrintAll();
Student::PrintStudent();
}

main.cpp

#include <iostream>
#include "head.h"
using std::cout;
using std::endl;

int main()
{
Building hotel(20,600,4000);
hotel.PrintAll();
cout<<endl;
House home(20,600,4000,400,1000);
home.PrintAll();
cout<<endl;
Office office(20,600,4000,996996,20);
office.PrintAll();
return 0;
}

运行截图

cbbf3be0d3c5eca7db526ae1ba8b7d5e.png

Author: Joe1sn
Link: http://blog.joe1sn.top/2020/c++HomeWork-10/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信