Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
// inheritance , polymorphism
#include<iostream>
#include<string>

using namespace std;

class person
{
	public :
		int age;
		string name;
		string get_name()
		{
			cout<<"Please enter the name"<<endl;
			cin>>name;
			return name;
		}
		int get_age()
		{
			cout<<"enter the age"<<endl;
			cin>>age;
			return age;
		}
};

class student : public person
{
  private : 
  int id;
  char section;
  public:
  int get_id(){
  	cout<<"enter the id of the student"<<endl;
  	cin>>id;
  	return id;
  }
  char get_section()
  {
  	cout<<"enter the section"<<endl;
  	cin>>section;
  	return section;
}	
};

int main()
{
int *a;
string n;
student s;
int i;
char sec;
char c;
n=s.get_name();
*a=s.get_age();
cout<<"is this person a student? please enter y or n"<<endl;
cin>>c;
if (c=='y')
{
	i=s.get_id();
	sec=s.get_section();
	cout<<" My name is "<<n<<" my age is "<<*a<<" my id is "<<i<<" my sec is "<<sec;
}
if (c=='n')
{

	cout<<"My name is "<<n<<" my age is "<<*a<<" I am not a student ";
}

}


What I have tried:

age is declared as *a in main function. how to assign it the value that get_age() is returning.
Posted
Updated 3-Feb-17 20:14pm

Your code is partly correct but missing important steps. In the class student the value age never gets set.
Two ways of doing this are in the constructor:
Constructors and member initializer lists - cppreference.com[^]

or by get and set methods:
The purpose of get and set methods in C++[^]

The second approach should suit you perfectly. Just add set methods to your class.
 
Share this answer
 
v4
Comments
Stefan_Lang 6-Feb-17 2:01am    
Well spotted.
As for your advice: "Just add set methods to your class" maybe you should point out that he also needs to invoke them ;-)
[no name] 6-Feb-17 2:22am    
That I sincerely hope is inferred. The OP's code clearly shows they know that although up to now they have been silent.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900