jeudi 13 août 2015

Why does my program crash in Visual Studio?

I am attempting to write a program for my programming course that demonstrates the use of inheritance. I build the program successfully and click start without debugging. The black box shows up normally. However the program crashes. The program should display all the variables within each of the different classes. Anyway, here is the code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class Person{
protected:
    string name;
    string address;
    string city;
    string state;
    int zip;
    int phoneNumber;
public:
    Person(){
        name = "JohnDoe";
        address = "1234ExampleDrive";
        city = "Orlando";
        state = "Florida";
        zip = 12345;
        phoneNumber = 1234567890;
    }

    void display();
};

class Student: public Person{
private:
    string grade;
    string course;
    float gpa;
public:
    Student(){
        grade = 'A';
        course = "Algebra";
        gpa = 3.8;
    }

    void display();
};

void Person::display(){
    cout<<name<<endl;
    cout<<address<<endl;
    cout<<city<<endl;
    cout<<state<<endl;
    cout<<zip<<endl;
    cout<<phoneNumber<<endl;
}

void Student::display(){
    display();
    cout<<grade<<endl;
    cout<<course<<endl;
    cout<<gpa<<endl;
}



int _tmain(int argc, _TCHAR* argv[])
{
    Student student1;
    student1.display();
    return 0;
}

Any help would be greatly appreciated, thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire