954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help on my inheritance here

help.why can't my coding loop properly.,..i notice it can't show the loop of ptrpers[j] ?
what did i do wrong.?
it didn't show back the input i put on earlier.

#include<iostream>
using namespace std;

class person
{
protected:
	char name[40];
public:
	void setname()
	{
		cout<<"Enter Name:";
		cin>>name;
	}
	
	void putname()
	{cout <<"Name:"<<name<<endl;}

	virtual void getdata()=0;//pure virtual or abstrak
	virtual bool outstanding()=0;//pure virtual or abstrak
};

class prof:public person
{
	int numpub;
public:
	void getdata()
	{
		person::setname();
		cout<<"Enter number of prof's publication:";
		cin>>numpub;
	}
	bool outstanding()
	{return(numpub>60)? true:false;}
};
class stud:public person
{
	int mark;
public:
	void getdata()
	{
		person::setname();
		cout<<"Enter student mark:";
		cin>>mark;
	}
	bool outstanding()
	{return(mark>60)? true:false;}
};
int main()
{
	person*ptrpers[100];
	int n=0;//i=0;
	char choice;

	do{
		cout<<"enter student or prof,(s/p):";
		cin>>choice;
		if (choice == 's')
		ptrpers[n] = new stud;
	
		else
		ptrpers[n] = new prof;
		
		ptrpers[n]-> getdata();

	cout<<"enter another prson(y/n)?";
	cin>>choice;

	}while(choice=='y');

	for(int j=0;j<n;j++)
	{
		ptrpers[j] -> putname();
		if(ptrpers[j] -> outstanding() )
		{
			cout<<"person outstanding\n";
		}
	}
	return 0;
}
MyRedz
Light Poster
30 posts since Jul 2007
Reputation Points: 7
Solved Threads: 0
 

Because you never increment "n" anywhere. In your do while, you need to stick an n++ some place.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

thanks i didn't realize that mistake.now it works like a charm!

MyRedz
Light Poster
30 posts since Jul 2007
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You