c++ - 需要有关 Visual C++ 中面向对象程序的帮助

标签 c++

伙计们,我有以下代码,但我在编译时遇到错误...错误在最后。谢谢 第一类是“人”

#ifndef PERSON_H//protecting .h files
#define PERSON_H//protecting .h files
#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
    Person();
    Person(string first, string last)
    {
        firstName = first;
        lastName = last;
    }
    virtual void setName(string first, string last)
    {
        firstName = first;
        lastName = last;
    }
    virtual void setWeightAge(int w, int a)
    {
        weight = w;
        age = a;
    }
    virtual string getFirstName()
    {
        return firstName;
    }
    virtual string getLastName()
    {
        return lastName;
    }
    virtual int getWeight()
    {
        return weight;
    }
    virtual int getAge()
    {
        return age;
    }
    virtual void printPerson()
    {
        cout << "Name: " << firstName << " " << lastName << endl;
        cout << "Age: " << age << endl;
        cout << "Weight: " << weight << endl;
    }
protected:
    string firstName, lastName;
    int weight, age;
};
#endif

第二类是“学生”

#ifndef STUDENT_H//protecting .h files
#define STUDENT_H//protecting .h files
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;

class Student : public Person
{
public: 
    Student();
    Student(Person s)
    {
        sStudent = s;
    }
    virtual void setGPA(double g)
    {
        gpa = g;
    }
    virtual void setSchedule(string c, string t, string d)
    {
        stClass = c;
        time = time;
        days = d;
    }
    virtual void setGrade(char g)
    {
        grade = g;
    }
    virtual double getGPA()
    {
        if (grade == 'a') { gpa = 4.0; }
        if (grade == 'b') { gpa = 3.0; }
        if (grade == 'c') { gpa = 2.0; }
        else gpa = 0;
        return gpa;
    }
    virtual char getGrade()
    {
        return grade;
    }

    virtual void printSchedule()
    {
        cout << "Class | Days | Time " << endl;
        cout << stClass << " | " << days << " | " << time << endl;
    }
protected:
    string stClass, time, days;
    char grade;
    double gpa;
    Person sStudent;
};
#endif

和主要()

#include <iostream>
#include "Student.h"
using namespace std;

int main()
{
    //creating a person
    Person john("John", "Smith");
    john.setWeightAge(180, 39);
    john.printPerson();

    //making john a student
    Student johnStdntMath(john);
    johnStdntMath.setSchedule("Math", "7:45", "M, W");
    johnStdntMath.setGrade('b');
    johnStdntMath.printPerson();
    johnStdntMath.printSchedule();

    system("pause");
    return 0;

错误:

1>------ Build started: Project: Person, Configuration: Debug Win32 ------
1>  main.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Person::Person(void)" (??0Person@@QAE@XZ) referenced in function "public: __thiscall Student::Student(class Person)" (??0Student@@QAE@VPerson@@@Z)
1>c:\users\jorge\documents\visual studio 2010\Projects\Person\Debug\Person.exe : fatal error LNK1120: 1 unresolved externals
}

最佳答案

我建议您仔细检查您的 is-A 和 has-A 关系。

写作 Student : public Person说学生是一个人。但是后来,你有一个成员变量 sStudent类型 Person ,上面写着一个 Student has-A Person,我猜这不是您真正想要的。

查看此问题的答案:Inheritance vs. Aggregation以获得更好的解释。

关于c++ - 需要有关 Visual C++ 中面向对象程序的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5024445/

相关文章:

c++ - ifstream.read 只读取文件的一半

c++ - 从基类指针集合中调用非虚拟成员函数?

c++ gmock使用相同的args调用其他函数

c++ - 即使我返回 -1 程序仍然运行?

c++ - Eigen :如果我只能计算 Aty 和 Ax,是否有可能创建类似 LeastSquareDiagonalPreconditioner 的调节器?

c++ - Linux make 无法链接到 Boost 非 header 库

c++ - VaSTLy 不同输出 C++ 蒙特卡洛近似

c++ - C++ 中的 Typedef 枚举

java - Android 编译 : No rule to make target

c++ - 为什么 char *x 而不是 char x?