需要帮助了解C ++类的语法

问题描述 投票:0回答:1

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

我实际上还没有被教过继承,但是由于学生和教师都需要相同的变量(姓名,年龄,住所等),我认为这是明智的。

尽管有一些问题:

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

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

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

c++ class object inheritance syntax
1个回答
0
投票

谢谢,我大部分都知道了...

  1. 我需要在StudentTeacher中创建构造函数,并使用与Person类相同的输入变量,然后在Person构造函数中调用Student构造函数:
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. 不太确定还在...
© www.soinside.com 2019 - 2024. All rights reserved.