473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Trouble with inheritance

I have a base class that has constructor Person(string name, int age)
and a derived class Empolyee(string job_title, int salary)

When I try to call it using new Employee(name, age, job_title, salary) the
compiler tells me no matching function for call to `Person::Person()'
Employee::Employee(string name, int age, string job_title, int salary)
{
Person(name, age);
Title=job_title;
Wage=salary;
}

Could someone tell me what I am doing wrong?
Sep 11 '05 #1
3 2671
"James" <a@a.a> wrote in message news:43********@dnews.tpgi.com.au...
I have a base class that has constructor Person(string name, int age)
and a derived class Empolyee(string job_title, int salary)

When I try to call it using new Employee(name, age, job_title, salary) the
compiler tells me no matching function for call to `Person::Person()'
Employee::Employee(string name, int age, string job_title, int salary)
{
Person(name, age);
Title=job_title;
Wage=salary;
}

Could someone tell me what I am doing wrong?


You should grab a C++ book and read about initialization lists:

Employee::Employee(string name, int age, string job_title, int salary)
: Person(name,age)
, Title(job_title)
, Wage(salary)
{ }
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Sep 11 '05 #2
ashucl
3
if you have
class person
{
int ...
string ...
public:
person(int, string);
};

class Empoyee: public person
{
int ...
String...
public:
Employee(int ,string,int, string)
{
Person(int ,string);
}
}

every derived object has a unamed contained object of the base class.
so it will try to call the default constructor first and then the parameterized constructor.
to avoid this either do
class Empoyee: public person
{
int ...
String...
public:
Employee(int ,string,int, string): Person(int ,string)//member initializer list
{
}
}

or provide a copy of default c'tor in the person class along with the parameterized.
Sep 11 '05 #3
"James" <a@a.a> wrote in message news:43********@dnews.tpgi.com.au
I have a base class that has constructor Person(string name, int age)
and a derived class Empolyee(string job_title, int salary)

When I try to call it using new Employee(name, age, job_title,
salary) the compiler tells me no matching function for call to
`Person::Person()'
Employee::Employee(string name, int age, string job_title, int salary)
{
Person(name, age);
Title=job_title;
Wage=salary;
}

Could someone tell me what I am doing wrong?


Yes. Base constructors must be called in the initialisation list:

Employee::Employee(string name, int age, string job_title, int salary) :
Person(name, age)
{
Title=job_title;
Wage=salary;
}

When you omit the Base constructor from the initialisation list, the default
constructor of Person() is called. Since you don't have a default
constructor, you get the error.

When you call Person(name, age); in the body of the Employee constructor,
this does *not* call the Base constructor. Instead, it creates a Person
temporary, which disappears immediately.
--
John Carson
Sep 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fernando Rodriguez | last post by:
Hi, I'm having trouble with a metaclass suposed to check the method signature of its classes. Here's the metaclass: class MetaChecker(type): def __new__(cls, name, bases, attribs): for...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
9
by: zippy747 | last post by:
I would like to have a base class that does some work and can be instantiated. class a { public: virtual int foo() { return 0; }; virtual int bar() { return 0; }; };
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
5
by: Larry Serflaten | last post by:
I am trying to build a chart to help me track what gets called when in the inheritance chain. I came across an error that has me puzzled. I have a derived class that inherits from a base class,...
3
by: Nathan Sokalski | last post by:
I am having trouble removing cookies that I created with my site. Here is the code I am using to try and remove them: If Not Request.Cookies("username") Is Nothing Then...
4
by: ingoweiss | last post by:
Hi, I am having trouble passing parameters of a Javascript subclass constructor through to it's superclass constructor. I am trying all sorts of things, including the below, but nothing...
0
by: rossabri | last post by:
This topic has been addressed in limited detail in other threads: "sockets don't play nice with new style classes :(" May 14 2005....
9
by: dylan.miller | last post by:
I'm having trouble understanding the internal access modifier. There are many classes in my assembly that should not be accessible outside of the assembly. I've used the internal access modifier...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.