Learning Classes/Inheritance - Need resolution by 10 pm 1/26/15

Hello everyone. I am new to programming and need help. Much of this program was built with my teacher and almost everything works. I am running into a problem with my IF-ELSE statement that is in cs_cis class. I noticed you will need to expand the window of this post for the program to format correctly. Whether I put true or false in the following statement in main(): cs_cis csStudent1(false, 1001); it outputs "CS Major" which you can see is in the IF statement in the cs_cis class. No matter true or false it will output "CS Major" and won't output "CIS Major" for false for example.

My terminology is still weak so please explain well if you can answer my question.














#include <string.h>
#include <string>
#include <iostream>
using namespace std;


class StudentInfo // This class works with the students first, last name and Id
{
int ID; // Private
string firstName;
string lastName;

public:
void getStudentInfo(string first, string last);
void setStudentInfo(string first, string last);

StudentInfo(int x) // Constructor function
{
ID = x;
}
};

class cs_cis : public StudentInfo{
private:
bool CS;
public:
cs_cis(bool CS, int ID):StudentInfo(ID){}

void showCS(){

if(CS == true)
{
cout << "CS Major";
}

else
{
cout << "CIS Major";
}

}
};

void StudentInfo::setStudentInfo(string first, string last)
{
firstName = first;
lastName = last;
}

void StudentInfo::getStudentInfo(string first, string last)
{
cout << ID << ", " << first << " " << last << ", ";
}

int main(){
string first, last;
cs_cis csStudent1(false, 1001);

cout << "Enter the first name:" << endl;
cin >> first;
cout << endl;

cout << "Enter the last name:" << endl;
cin >> last;
cout << endl;


csStudent1.setStudentInfo(first, last);
csStudent1.getStudentInfo(first, last);
csStudent1.showCS();


return 0;
}
Your constructor for the cs_cis class is empty. You need to write the code for the constructor.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <string.h>
#include <string>
#include <iostream>
using namespace std;

class StudentInfo
{
    int ID;
    string firstName, lastName;
public:
    void getStudentInfo() const;
    void setStudentInfo(const string& first, const string& last);

    StudentInfo(int x): ID(x){}
};

class cs_cis : public StudentInfo
{
    bool CS;
public:
    cs_cis(bool CS, int ID):StudentInfo(ID){this -> CS = CS;}

    void showCS()
    {
        if(CS == true)
            cout << "CS Major";
        else
            cout << "CIS Major";
    }
};

void StudentInfo::setStudentInfo(const string& first, const string& last)
{
    firstName = first;
    lastName = last;
}

void StudentInfo::getStudentInfo() const
{
    cout << ID << ", " << firstName << " " << lastName << ", ";
}

int main()
{
    string first, last;
    cs_cis csStudent1(false, 1001);

    cout << "Enter the first name: ";
    cin >> first;
    cout << "Enter the last name: ";
    cin >> last;
    cout << endl;

    csStudent1.setStudentInfo(first, last);
    csStudent1.getStudentInfo();
    csStudent1.showCS();

    return 0;
}
Topic archived. No new replies allowed.