C++ 需要帮助理解C++;班级

C++ 需要帮助理解C++;班级,c++,class,object,inheritance,syntax,C++,Class,Object,Inheritance,Syntax,我已经完成了哈佛CS50课程,并与python进行了相当多的合作。我现在正在做一个C++课程(微软的),学习练习让我创建了几个类。这个练习特别要求我在main中实例化几个对象并返回一些值 #include <iostream> #include "School.h" int main() { Student student1("Joe", "Bloggs", 31, "123 Fake Street", "London"); Student student2("Fr

我已经完成了哈佛CS50课程,并与python进行了相当多的合作。我现在正在做一个C++课程(微软的),学习练习让我创建了几个类。这个练习特别要求我在main中实例化几个对象并返回一些值

#include <iostream>
#include "School.h"

int main()
{
    Student student1("Joe", "Bloggs", 31, "123 Fake Street", "London");
    Student student2("Fred", "Adams", 24, "95 Something Avenue", "Manchester");
    Student student3("John", "Doe", 90, "44a Old Man Lane", "Kettering");

    Course course1("Introduction to Psychology");
    course1.addStudent(student1);
    course1.addStudent(student2);
    course1.addStudent(student3);

    Teacher teacher1("Helen", "Professorson", 48, "15 Teacher Way", "Kent");

    course1.addTeacher(teacher1);

    std::cout << course1.getCourseName() << std::endl;
    teacher1.GradeStudent();
}
#包括
#包括“School.h”
int main()
{
学生1(“乔”,“布洛格斯”,31,“假街123号”,“伦敦”);
学生2(“弗雷德”、“亚当斯”、24、“曼彻斯特大道95号”);
学生3(“约翰”、“多伊”、90、“44a老人巷”、“凯特林”);
课程1(“心理学导论”);
课程1.添加学生(学生1);
课程1.增加学生(学生2);
课程1.增加学生(学生3);
教师1(“海伦”,“教授”,48,“15教师之路”,“肯特”);
课程1.增加教师(教师1);

谢谢,我基本上都明白了

  • 我需要在
    Student
    Teacher
    中使用与
    Person
    类相同的输入变量创建构造函数,然后在
    Student
    构造函数中调用
    Person
    构造函数:
  • 您不需要在构造函数中指定数组大小: 头文件如下所示:
  • 构造函数如下所示:

    Course::Course()
        : _students{ 0 }, _teacher{ 0 }, _name{}
    {
    }
    
    将每个数组项初始化为0

  • 还是不太确定
  • 听起来您可以使用
    ~Person();
    +空实现,应该是
    ~Person()=default;
    而不使用用户定义的实现。空的用户定义析构函数与编译器生成的析构函数不同。您的代码是次优的(析构函数不是“”)。
    #include "School.h"
    #include <iostream>
    #include <string>
    
    // Constructors
    Person::Person()
    {
        std::string _fName{};
        std::string _lName{};
        int _age{};
        std::string _address{};
        std::string _city{};
        std::string _phone{};
    }
    
    Person::Person(std::string fName, std::string lName)
    {
        std::string _fName{ fName };
        std::string _lName{ lName };
        int _age{};
        std::string _address{};
        std::string _city{};
        std::string _phone{};
    }
    
    Person::Person(std::string fName, std::string lName, int age, std::string address, std::string city, std::string phone)
    {
        std::string _fName{ fName };
        std::string _lName{ lName };
        int _age{ age };
        std::string _address{ address };
        std::string _city{ city };
        std::string _phone{ phone };
    }
    
    // Destructor
    Person::~Person()
    {
    }
    
    std::string Person::getFirstName()
    {
        return this->_fName;
    }
    
    void Person::setFirstName(std::string fName)
    {
        this->_fName = fName;
    }
    
    std::string Person::getLastName()
    {
        return this->_lName;
    }
    
    void Person::setLastName(std::string lName)
    {
        this->_lName = lName;
    }
    
    int Person::getAge()
    {
        return this->_age;
    }
    
    void Person::setAge(int age)
    {
        this->_age = age;
    }
    
    std::string Person::getAddress()
    {
        return this->_address;
    }
    
    void Person::setAddress(std::string address)
    {
        this->_address = address;
    }
    
    std::string Person::getCity()
    {
        return this->_city;
    }
    
    void Person::setCity(std::string city)
    {
        this->_city = city;
    }
    
    std::string Person::getPhone()
    {
        return this->_phone;
    }
    
    void Person::setPhone(std::string phone)
    {
        this->_phone = phone;
    }
    
    void Student::SitInClass()
    {
        std::cout << "Sitting in main theater" << std::endl;
    }
    
    void Teacher::SitInClass()
    {
        std::cout << "Sitting at front of class" << std::endl;
    }
    
    void Teacher::GradeStudent()
    {
        std::cout << "Student Graded" << std::endl;
    }
    
    Course::Course()
    {
        Student* _students;
        Teacher* _teacher;
        std::string _name{};
    }
    
    Course::Course(std::string name)
    {
        Student* _students[30];
        Teacher* _teacher;
        std::string _name{ name };
    }
    
    Course::~Course()
    {
    }
    
    void Course::addStudent(Student student)
    {
        // TODO: Loop through _students and insert new student in
    }
    
    void Course::addTeacher(Teacher teacher)
    {
        this->_teacher = &teacher;
    }
    
    std::string Course::getCourseName()
    {
        return this->_name;
    }
    
    void Course::setCourseName(std::string name)
    {
        this->_name = name;
    }
    
    Student::Student()
    {
    }
    
    Student::Student(std::string fName, std::string lName)
        : Person(fName, lName)
    {
    }
    
    Student::Student(std::string fName, std::string lName, int age, std::string address, std::string city, std::string phone)
        : Person(fName, lName, age, address, city, phone)
    {
    }
    
    class Course
    {
        ...
    
    private:
        // Member variables
        std::string _name;
        Student* _students[30];
        Teacher* _teacher;
    };
    
    Course::Course()
        : _students{ 0 }, _teacher{ 0 }, _name{}
    {
    }