C++面向对象实验三

实验三

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();

    return 0;C++面向对象实验三_第1张图片

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;}

a8bafc36934c40e39427771f4f06a3ce.png

 

//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;

}

01a91d807a6f4de4bd786061523c596a.png

 

 

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 courses_;

};

 

#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;

}

C++面向对象实验三_第2张图片

 

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();

                     cout<

}

private:

              int a;

              B3 bb;

};

 

 

void main()

{

       A aa(1, 2, 3, 4);

       aa.print();

        }

 

 

这段C++程序定义了一些类(B1、B2、B3和A)以及在、main()函数中创建-一个A类的对象aa并调用相关的函数。下面是程序的分析和运行结果:

 

1.类B1、B2、B3分别具有构造函数,用于初始化成员变量b1、b2和b3,并在构造函数中输出相关的信息;2.类A继承了类B2和类B1,同时有- -个B3类型的成员变量bb。在A类的构造函数中,会调用B1和B2的构造函数来初始化基类成员,并初始化A类的成员a和bb;

 

3.在main()函数中,创建了一个A类的对象aa,传入了4个整数参数:1、2、3、4这些参数分别用于初始化B1、B2、B3和A类的成员;

 

4.在创建A对象时,首先会调用B1和B2的构造函数,然后调用B3的构造函数,最后调用A的构造函数。构造函数的输出语句将显示相应的信息;

 

5.在main(函数中,调用了aa的print()函数。在、print()函数中,首先调用B1和B2的、print()函数,然后输出A类的成员a;

 

C++面向对象实验三_第3张图片

 

你可能感兴趣的:(c++,算法,开发语言)