Cout没有打印出预期的结果

时间:2018-02-05 02:37:47

标签: c++

当我编译以下代码时,由于某种原因,学生/教师的姓名,年龄和GPA /等级不会通过各自的printPerson函数返回。

使用名称变量,不会向控制台打印任何内容。 随着年龄,GPA /评级,控制台打印出负8位数字和负浮动。

我没看到什么?

Person.h

#pragma once
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <vector>
#include <string>


using std::string;

// Base class
class Person {

protected:
    string name;
    int age;

public:
    void setName(string name);
    void setAge(int age);
    virtual void do_work(int number) {};
    virtual void printPerson() {};
};

#endif;

Person.cpp

#include "Person.h"
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::cin;
using std::endl;


void Person::setName(string name) {
    name = name;
}

void Person::setAge(int age) {
    age = age;
}

Student.h

#pragma once
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"

using std::string;

class Student : public Person {

private:
    float gpa;


public:
    void setGPA(float gpa);
    float getGPA();
    void do_work(int number);
    void printPerson();
};

#endif;

Student.cpp

#include "Student.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;



 void Student::setGPA(float gpa) {
       gpa = gpa;
 }

 float Student::getGPA() {
       return gpa;
 }

 void Student::do_work(int number) {
       //cout << name << ".. " << number << "hours of homework.” << endl;
       cout << name;
 }

 void Student::printPerson() {
       cout << "Name : " << name << "Age :" << age << " GPA : " << getGPA() << endl;
}

Instructor.h

#pragma once
#ifndef INSTRUCTOR_H
#define INSTRUCTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"

class Instructor : public Person {

private:
    float rating;

public:
    void setRating(float rating);
    float getRating();
    void do_work(int number);
    void printPerson();
};

#endif;

Instructor.cpp

#include "Instructor.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

void Instructor::setRating(float rating) {
    rating = rating;
}

float Instructor::getRating() {
    return rating;
}

void Instructor::do_work(int number) {
    cout << name << "graded papers for" << number << "hours." << endl;
}

void Instructor::printPerson() {
    cout << " Name : " << name << " Age : " << age << " Rating : " << getRating() << endl;
}

University.h

#pragma once
#ifndef UNIVERSITY_H
#define UNIVERSITY_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
#include "Building.h"
#include "Student.h"


using std::cout;
using std::string;
using std::vector;

class University {

public:
    string name;
    vector<Person*> persons;
    vector<Building> buildings;

public:
    void printAllBuildings();
    void printAllPersonsRecord();
};

#endif;

University.cpp

#include "University.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

void University::printAllBuildings() {
    cout << " Building Details : " << endl;
    for (int j = 0; j < buildings.size(); j++) {
       buildings[j].printBuilding();
    }
}

void University::printAllPersonsRecord() {
    cout << " Persons Details : " << endl;
    for (int i = 0; i < persons.size(); i++) {
       persons[i]->printPerson();
    }
}

Building.h

#pragma once
#ifndef BUILDING_H
#define BUILDING_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"

class Building {
public:
    string name;
    int size;
    string address;

public:
    void printBuilding();
};

#endif;

Building.cpp

#include "Building.h"
#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

void Building::printBuilding() {
    cout << " Name : " << name << " Address : " << address << endl;
}

的main.cpp

#include "University.h"
#include "Person.h"
#include "Student.h"
#include "Instructor.h"

#include <iostream>
#include <vector>
#include <string>


using std::string;
using std::cout;
using std::cin;
using std::endl;

int main() {

    Student student;
    Instructor instructor;

    student.setName("deepak");
    student.setAge(12);
    student.setGPA(12.0);

    instructor.setName("rajdev");
    instructor.setAge(23);
    instructor.setRating(5.0);

    Building building;

    building.name = "block1";
    building.size = 2000;
    building.address = "noida sector-2";

    Building building2;

    building2.name = "block2";
    building2.size = 4000;
    building2.address = "noida sector-2";

    University university;

    university.name = "Oregon State University";
    university.persons.emplace_back(&student);
    university.persons.emplace_back(&instructor);
    university.buildings.push_back(building);
    university.buildings.push_back(building2);

    university.printAllBuildings();

    university.printAllPersonsRecord();

    int choice;
    bool isValidMainChoice = false;
    while (!isValidMainChoice) {
       cout << "Kindly choose one of the option from follwoing list of operations or Menu" << endl;
       cout << "1 : Prints names of all the buildings" << endl;
       cout << "2 : Prints names of everybody at the university" << endl;
       cout << "3 : Choose a person to do work" << endl;
       cout << "4 : Exit the program" << endl;
       cin >> choice;
       cout << "The value you entered is " << choice << endl;
       if (choice == 1) {
          university.printAllBuildings();
       }
       else if (choice == 2) {
          university.printAllPersonsRecord();
       }
       else if (choice == 3) {
          int personChoice;
          bool isInputValid = false;
          while (!isInputValid) {
             cout << "Kindly choose the one of the following option to provide person's details." << endl;
             cout << "5 : Student" << endl;
             cout << "6 : Instructor" << endl;
             cin >> personChoice;
             if (personChoice == 5) {
                isInputValid = true;
                string studentName;
                bool isValidName = false;
                while (!isValidName) {
                    cout << " Kindly enter Name of the student :" << endl;
                    cin >> studentName;
                    if (studentName.length() == 0) {
                       cout << " Name must not be blank. Kindly re-enter the student's name." << endl;
                    }
                    else {
                       isValidName = true;
                    }
                }
                int age1 = 0;
                bool isValidAge1 = false;
                while (!isValidAge1) {
                    cout << " Kindly enter age of the student :" << endl;
                    cin >> age1;
                    if (age1 < 0 || age1 > 100) {
                       cout << " Age must be geter than 0 or lessa then 100. Kindly re-enter the student's age." << endl;
                    }
                    else {
                       isValidAge1 = true;
                    }
                }
                float gpa;
                bool isValidGPA = false;
                while (!isValidGPA) {
                    cout << " Kindly enter GPA of the student :" << endl;
                    cin >> gpa;
                    if (gpa < 0.0 || gpa > 4.0) {
                       cout << " GPA must be geter than 0.0 or less then 4.0. Kindly re-enter the Student GPA." << endl;
                       isValidGPA = false;
                    }
                    else {
                       isValidGPA = true;
                    }
                }
                Student student;
                student.setName(studentName);
                student.setAge(age1);
                student.setGPA(gpa);
                university.persons.emplace_back(&student);
                university.printAllPersonsRecord();
             }
             else if (personChoice == 6) {
                isInputValid = true;
                string instructorName;
                bool isValidName = false;
                while (!isValidName) {
                    cout << " Kindly enter Name of the instructor :" << endl;
                    cin >> instructorName;
                    if (instructorName.length() == 0) {
                       cout << " Name must not be blank. Kindly re-enter the instructor's name." << endl;
                    }
                    else {
                       isValidName = true;
                    }
                }

                float rating;
                bool isValidRating = false;
                while (!isValidRating) {
                    cout << " Kindly enter rating of the instructor :" << endl;
                    cin >> rating;
                    if (rating < 0.0 || rating > 5.5) {
                       cout << " rating must be geter than 0.0 or less then 5.5. Kindly re-enter the instructor rating." << endl;
                       isValidRating = false;
                    }
                    else {
                       isValidRating = true;
                    }
                }
                int age2 = 0;
                bool isValidAge2 = false;
                while (!isValidAge2) {
                    cout << " Kindly enter age of the instructor :" << endl;
                    cin >> age2;
                    if (age2 < 0 || age2 > 100) {
                       cout << " Age must be geter than 0 or lessa then 100. Kindly re-enter the instructor's age." << endl;
                    }
                    else {
                       isValidAge2 = true;
                    }
                }
                Instructor instructor;
                instructor.setName(instructorName);
                instructor.setAge(age2);
                instructor.setRating(rating);
                university.persons.emplace_back(&instructor);
             }
             else {
                cout << "The value you entered is incorrct.Please r-enter the values." << endl;
             }
          }
       }
       else if (choice == 4) {
          isValidMainChoice = true;
          cout << " You are exits from system. Thanks You !!" << endl;
       }
    }
    return 0;
};

1 个答案:

答案 0 :(得分:1)

请修改 setter

的所有代码
void Person::setName(string name) {
    //before-edit: name = name;
    this->name = name; //OR Person::name = name;
}

由于本地字符串 名称 参数和您的类变量相同,您希望参数正确传递,但它不会。