I wanted to define a constructor for a derived class and use the base class constructor which I have defined. I have commented the derived class constructor code.
#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include "string"
using namespace std;
class person{
private:
string name;
int age;
public :
person(int,string); //constructor
};
class student : public person{ //derived class
private :
string teacher;
public :
student(string);
};
person :: person(int newage,string newname){
age = newage;
name = newname;
cout <<age << name;
}
/* How do I define the derived class constructor , so that by default
it calls base class person(int,string) constructor.
student :: student(string newteacher){
teacher = newteacher;
cout<<teacher;
}
*/
int _tmain(int argc, _TCHAR* argv[])
{
person p(20,"alex");
student("bob");
return 0;
}
Adding more details:
I want to define my derived class constructor in a way , that I can call base class constructor within my derived class constructor.Right now if I uncomment my derived class constructor I get the following error "no default constructor exists for class person.". Is it possible to do something like:
student object("name",10,"teacher_name")
name ,age should be initialized using base class constructor and teacher_name should be initialized using derived class constructor. I am new to C++ , so if something like this is not possible , please let me know.