简体   繁体   English

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

[英]Need help understanding the syntax of C++ Classes

I've done the Harvard CS50 Course and worked with python a fair bit.我已经完成了哈佛 CS50 课程,并与 python 一起工作。 I'm now doing a C++ course (Microsoft's) and the learning exercise has me creating a few classes.我现在正在学习 C++ 课程(微软的),学习练习让我创建了一些课程。 The exercise specifically tasks me to instantiate a few objects in main and return some values;该练习专门要求我在 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();
}

I'm using a header file and cpp file to define the classes, School.h:我正在使用 header 文件和 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:学校.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;
}

I actually haven't been taught inheritance yet, but since both Student and Teacher needed the same variables (name, age, address etc.) I decided it'd be sensible.实际上,我还没有学过 inheritance,但由于学生和教师都需要相同的变量(姓名、年龄、地址等),我认为这是明智的。

Having a few problems though:虽然有几个问题:

  1. My instantiations of Student and Teacher in main.cpp aren't correct.我在 main.cpp 中的StudentTeacher实例不正确。 I think because they are inheriting from Person(?).我认为是因为它们是从 Person(?) 继承的。 How do I create a constructor within these derived classes?如何在这些派生类中创建构造函数? I googled this but the solutions didn't seem to work.我用谷歌搜索了这个,但解决方案似乎没有用。

  2. The Course Class requires an array of Students. Course Class 需要一组学生。 Do I have to specify a size of the array in the header file?是否必须在 header 文件中指定数组的大小? It seems silly that I've specified 30 in both the header file and the cpp file, but VSstudio complains if I don't.我在 header 文件和 cpp 文件中都指定了 30 似乎很愚蠢,但如果我不这样做,VSstudio 会抱怨。

  3. I'm using strings here.我在这里使用字符串。 I have previously learned from a different course about char* and memory regarding strings.我之前从另一门关于 char* 和 memory 的关于字符串的课程中学习过。 How many chars are assigned to all of these string class variables?为所有这些字符串 class 变量分配了多少个字符? If I instantiate a Students with name "Joe" and then want to later change his name to "Joseph" using student1.SetFirstName , is that going to cause segmentation faults?如果我实例化一个名为“Joe”的学生,然后想稍后使用student1.SetFirstName将他的名字更改为“Joseph”,这会导致分段错误吗?

Thanks, I figured it out mostly...谢谢,我主要是想通了...

  1. I needed to create constructors in Student and Teacher with the same input variables as the Person class, and then call the Person constructor in the Student consturctor:我需要在StudentTeacher中使用与Person class 相同的输入变量创建构造函数,然后在Student构造函数中调用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. You do not need to specify array size in the constructor: Header file looks like this:您不需要在构造函数中指定数组大小:Header 文件如下所示:
class Course
{
    ...

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

And the constructor looks like this:构造函数如下所示:

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

To initialise every array item to 0.将每个数组项初始化为 0。

  1. Not so sure on still...还不是很确定...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM