It looks like you might be a Python programmer, so here is your code, re-written in that langage
class student:
def __init__(self, fname, lname):
self.firstname = fname;
self.lastname = lname;
@property
def name(self):
return self.firstname + self.lastname
class undergraduate(student):
def __init__(self, fname, lname, gpa):
super().__init__(fname, lname)
self.gpa = gpa
stu1 = undergraduate("Sam", "Singh", 4.0);
stu1.firstname = "temp";
print(stu1.name)
The first thing to notice is that the undergraduate
does not contain a student
member. Since it inherits from student
(is a) there is no need for a member in undergraduate
. It's the same for the C++ class.
However, in the Python code, the undergraduate
class calls the student
ctor in the body of the undergraduate
ctor. That's different from how it works in C++. That language uses "initializer lists". These are used to not only call parent ctors but also initialize member variables.
class student {
private: // You may want to change to protected so child classes can access
string firstname;
string lastname;
public:
student(string fname, string lname) {
firstname = fname;
lastname = lname;
}
string getname() { return firstname + lastname; }
void setfirstname(string fname) { this->firstname = fname; }
};
class undergraduate : public student {
private:
double gpa;
public:
undergraduate(string firstname, string lastname, double gpa) :
student(firstname, lastname), // Call parent ctor
gpa(gpa) // Initialize this->gpa to gpa parameter
{
// The initializer list has taken care of everything so there's nothing here
// But additional code could be added if needed.
}
};
int main() {
undergraduate stu1("Sam", "Singh", 4.0);
stu1.setfirstname("temp");
cout << stu1.getname();
}
If you are confused about how inheritance works, I strongly recommend you find a good book or tutorial. Answers on SO cannot go into enough detail to fully explain the concepts.
The Definitive C++ Book Guide and List
Constructors and member initializer lists
RE the original error in the code: the compiler is looking for a default ctor (no parameters) for student
but you do not provide one. See: When do we need to have a default constructor?