I have no error or warning when compling this code but as you can see it keeps changing one of my inputs (int ID) into a random number. This same number is still the output when i change the imput.

code:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

class student
{
protected:
	string name;
	int ID;
public:
	student()
	{
		name = "no name";
		ID = 0;
	}
	student (string pname,int pID)
	{
		name = pname;
		ID = pID;
	}
	~student(){}
	//virtual void add()=0;
	virtual void print()=0;
};

class physstd : public student
{
private:
	string course;
	string grade;
	int percentage;
public:
	physstd()
	{
		course = "Null";
		grade = "Null";
		percentage = 0;
	}
	physstd(string name,int pID,string pcourse, string pgrade, int ppercentage):student(name,ID)
	{
		course = pcourse;
		grade = pgrade;
		percentage = ppercentage;
	}
	~physstd(){}
	//void add();
	void print()
	{
		cout<<"| "<<name<<" | "<<ID<<" | "<<course<<" | "<<grade<<" | "<<percentage<<"% |"<<endl<<endl;
	}
};
class chemstd : public student
{
private:
	string course;
	string grade;
	int percentage;
public:
	chemstd()
	{
		course = "Null";
		grade = "Null";
		percentage = 0;
	}
	chemstd(string name,int pID,string pcourse, string pgrade, int ppercentage): student(name,ID)
	{
		course = pcourse;
		grade = pgrade;
		percentage = ppercentage;
	}
	~chemstd(){}
	//void add();
	void print()
	{
		cout<<"| "<<name<<" | "<<ID<<" | "<<course<<" | "<<grade<<" | "<<percentage<<"% |"<<endl<<endl;
	}
};

class mathstd : public student
{
private:
	string course;
	string grade;
	int percentage;
public:
	mathstd()
	{
		course = "Null";
		grade = "Null";
		percentage = 0;
	}
	mathstd(string name,int pID,string pcourse, string pgrade, int ppercentage): student(name,ID)
	{
		course = pcourse;
		grade = pgrade;
		percentage = ppercentage;
	}
	~mathstd(){}
	//void add();
	void print()
	{
		cout<<"| "<<name<<" | "<<ID<<" | "<<course<<" | "<<grade<<" | "<<percentage<<"% |"<<endl<<endl;
	}
};
int main()
{
	student *pp;
	physstd s;
	physstd s1("Terence Britton",5,"C++","A*",100);
	pp=&s;		pp->print();
	pp=&s1;		pp->print();

	return 0;
}

output:

| no name | 0 | Null | 0% |

| Terence Britton | -858993460 | C++ | A* | 100%

Obviously -858993460 is not what i entered in the main, which was 5. Any help with this would be much appreciated

thanks

Recommended Answers

All 2 Replies

In the derived constructors, you want to to pass the pID to the base class.

Thanks very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.