【问题标题】:Need help understanding the syntax of C++ Classes需要帮助理解 C++ 类的语法
【发布时间】:2019-10-22 16:18:18
【问题描述】:

我已经完成了哈佛 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();
}

我正在使用头文件和 cpp 文件来定义类,School.h:

#pragma once

#include <string>

class Person
{
public:
    // Constructors and Destructors
    Person();
    Person(std::string, std::string);   // First and Last Name
    Person(std::string, std::string, int, std::string, std::string, std::string); // fName, lName, Age, Address, City, Phone
    ~Person();

    // Member functions
    std::string getFirstName();
    void setFirstName(std::string);

    std::string getLastName();
    void setLastName(std::string);

    int getAge();
    void setAge(int);

    std::string getAddress();
    void setAddress(std::string);

    std::string getCity();
    void setCity(std::string);

    std::string getPhone();
    void setPhone(std::string);

private:
    std::string _fName;
    std::string _lName;
    int _age;
    std::string _address;
    std::string _city;
    std::string _phone;

};

class Student : public Person
{
public:
    // Constructors and Destructors


    // Member Functions
    void SitInClass();
};

class Teacher : public Person
{
public:
    // Member Functions
    void SitInClass();
    void GradeStudent();
};

class Course
{
public:
    // Constructors and Desctructors
    Course();
    Course(std::string); // course name
    ~Course();

    // Member functions
    void addStudent(Student);
    void addTeacher(Teacher);

    // Getters and Setters
    std::string getCourseName();
    void setCourseName(std::string);


private:
    // Member variables
    std::string _courseName;
    Student _students[30];
    Teacher _teacher;
};

School.cpp:

#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 和 Teacher 都需要相同的变量(姓名、年龄、地址等),所以我认为这是明智的。

虽然有一些问题:

  1. 我在 main.cpp 中对 Student 和 Teacher 的实例化不正确。我认为是因为它们是从 Person(?) 继承的。如何在这些派生类中创建构造函数?我用谷歌搜索了这个,但解决方案似乎不起作用。

  2. Course 类需要一组学生。我是否有在头文件中指定数组的大小?我在头文件和cpp文件中都指定了30似乎很愚蠢,但是如果我不这样做,VSstudio会抱怨。

  3. 我在这里使用字符串。我之前从另一门关于 char* 和关于字符串的内存的课程中学习过。为所有这些字符串类变量分配了多少个字符?如果我实例化一个名为“Joe”的学生,然后想稍后使用student1.SetFirstName 将他的名字更改为“Joseph”,这会导致分段错误吗?

【问题讨论】:

  • 听起来你可以使用good C++ book
  • ~Person(); + 空实现,应该是~Person() = default; 没有用户定义的实现。一个空的用户定义的析构函数不与编译器生成的析构函数相同。您的代码次优(析构函数不是“trivial”)。

标签: c++ class object inheritance syntax


【解决方案1】:

谢谢,我主要是想通了...

  1. 我需要在Student和Teacher中创建构造函数,输入变量与Person类相同,然后在Person构造函数中调用Person构造函数:
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)
{
}
  1. 您不需要在构造函数中指定数组大小: 头文件如下所示:
class Course
{
    ...

private:
    // Member variables
    std::string _name;
    Student* _students[30];
    Teacher* _teacher;
};

而构造函数是这样的:

Course::Course()
    : _students{ 0 }, _teacher{ 0 }, _name{}
{
}

将每个数组项初始化为 0。

  1. 还是不太确定...

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    相关资源
    最近更新 更多