实验三
1
//clock.h
#pragma once
#ifndef CLOCK_H
#define CLOCK_H
class Clock {
private:
int hour, minute, second;
public:
Clock(int h = 0, int m = 0, int s = 0);
void settime(int h, int m, int s);
void display();
};
#endif
//clock.cpp
#include "Clock.h"
#include
using namespace std;
Clock::Clock(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
void Clock::settime(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
void Clock::display() {
cout << hour << ":" << minute << ":" << second << endl;
}
//main.cpp
#include
#include "Clock.h"
#include
using namespace std;
int main() {
//1
Clock clock1(10, 10, 10);
clock1.display();
Clock clock2(0, 0, 0);
clock2.display();
clock2.settime(23,59,59);
Clock b(clock2);
clock2.display();
2
MyClass.h
#pragma once
#ifndef MYCLASS_H
#define MYCLASS_H
#include
using namespace std;
class MyClass {
private:
int maxInt;
double product;
bool isSame;
public:
MyClass();
MyClass(int a, int b);
MyClass(double a, double b, double c);
MyClass(string a, string b);
int getMaxInt();
double getProduct();
bool getIsSame();
};
#endif
Main.cpp
#include "MyClass.h"
using namespace std;
MyClass obj1(3, 6);
cout << "最大的整数是: " << obj1.getMaxInt() << endl;
MyClass obj2(1.1, 2.2, 3.3);
cout << "三个double数的乘积是: " << obj2.getProduct() << endl;
MyClass obj3("abcd", "abcdef");
cout << "两个字符串是否相同: " << obj3.getIsSame() << endl;
return 0;}
//sale.h
#include
using namespace std;
class store {
public:
//使用参数初始化表定义构造函数
store(int n, int q, float p) :num(n), quantity(q), price(p) {};
void total();
//静态成员函数求平均,以及显示结果
static float average();
static void display();
private:
int num;
int quantity;
float price;
//定义静态成员变量
static float discount;
static float sum;
static int n;
};
//类外初始化静态成员变量
float store::discount = 0.98;
float store::sum = 0;
int store::n = 0;
//类外定义成员函数
void store::total() {
if (quantity > 10)
sum += quantity * price * discount;
else
sum += quantity * price;
n += quantity;
}
float store::average() {
return sum / n;
}
void store::display() {
//调用了average函数
cout << "总销售款数为:" << sum << "平均售价为:" << store::average() << endl;
}
//main
#include
#include"Sale.h"
using namespace std;
int main{
store s[3] = {
store(101,5,23.5),
store(102,12,24.56),
store(103,100,21.5)
};
//使用for循环进行求和
for (int i = 0; i < 3; i++)
s[i].total();
//显示总金额和平均售价
store::display();
return 0;
}
3.
// person.h
#ifndef PERSON_H
#define PERSON_H
#include
using namespace std;
class person {
public:
person(int id, string name);
void Write();
void Display();
protected:
int id_;
string name_;
};
#endif
// person.cpp
#include "person.h"
#include
using namespace std;
person::person(int id, string name) : id_(id), name_(name) {}
void person::Write() {
cout << "请输入编号和姓名:" << endl;
cin >> id_ >> name_;
}
void person::Display() {
cout << "编号:" << id_ << endl;
cout << "姓名:" << name_ << endl;
}
// student.h
#ifndef STUDENT_H
#define STUDENT_H
#include "person.h"
class student : public person {
public:
student(int id, string name, string cls);
void Write();
void Average();
void Display();
private:
string cls_;
double math_, obj_, data_, ave_;
};
#endif
// student.cpp
#include "student.h"
#include
using namespace std;
student::student(int id, string name, string cls) : person(id, name), cls_(cls) {}
void student::Write() {
person::Write();
cout << "请输入班级:" << endl;
cin >> cls_;
cout << "请输入数学、面向对象、数据结构三门课程的成绩:" << endl;
cin >> math_ >> obj_ >> data_;
}
void student::Average() {
ave_ = (math_ + obj_ + data_) / 3;
}
void student::Display() {
person::Display();
cout << "班级:" << cls_ << endl;
cout << "数学成绩:" << math_ << endl;
cout << "面向对象成绩:" << obj_ << endl;
cout << "数据结构成绩:" << data_ << endl;
cout << "平均成绩:" << ave_ << endl;
}
// teacther.h
#ifndef TEACHER_H
#define TEACHER_H
#include "person.h"
#include
using namespace std;
class teacher : public person {
public:
teacher(int id, string name, string title, string dept);
void Write();
void Display();
private:
string title_;
string dept_;
vector
};
#endif
// teacher.cpp
#include "teacher.h"
#include
using namespace std;
teacher::teacher(int id, string name, string title, string dept) : person(id, name), title_(title), dept_(dept) {}
void teacher::Write() {
person::Write();
cout << "请输入职称和部门:" << endl;
cin >> title_ >> dept_;
cout << "请输入讲授课程编号(以-1结束):" << endl;
int course;
while (cin >> course && course != -1) {
courses_.push_back(course);
}
}
void teacher::Display() {
person::Display();
cout << "职称:" << title_ << endl;
cout << "部门:" << dept_ << endl;
cout << "讲授课程编号:";
for (int i = 0; i < courses_.size(); i++) {
cout << courses_[i] << " ";
}
cout << endl;
}
// main.cpp
#include "person.h"
#include "student.h"
#include "teacher.h"
#include
using namespace std;
int main() {
student s(0, "", "");
s.Write();
s.Average();
s.Display();
teacher t(0, "", "", "");
t.Write();
t.Display();
return 0;
}
5//
#include
using namespace std;
class B1
{
public:
B1(int i)
{
b1 = i;
cout<<"构造函数 B1."< } void print() { cout< private: int b1; }; class B2 { public: B2(int i) { b2 = i; cout<<"构造函数 B2."< } void print() { cout< private: int b2; }; class B3 { public: B3(int i) { b3 = i; cout<<"构造函数 B3."< } int getb3() { return b3; } private: int b3; }; class A : public B2, public B1 { public: A(int i, int j, int k, int l):B1(i), B2(j), bb(k) { a = l; cout<<"构造函数 A."<
} void print() { B1::print(); B2::print();