473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple Inheritance problem

How I can fix this problem?
I don't know why it alway prompted (first use this function). I have
use newDollars, newCents... to access the base class member variable.
And the member functions for Initialize() and Print(), is it function
overloading? it already have this functions in the base class.

Pls let me know what problem of its! Thx

------------------------------------------------------------------
Result
------------------------------------------------------------------
[admin Lab5]$ g++ -c main.cpp MoneyType.cpp ExtMoney.cpp
ExtMoney.cpp: In method `void ExtMoney::Print () const':
ExtMoney.cpp:22 : `newDollars' undeclared (first use this function)
ExtMoney.cpp:22 : (Each undeclared identifier is reported only once
ExtMoney.cpp:22 : for each function it appears in.)
ExtMoney.cpp:22 : `newCents' undeclared (first use this function)
ExtMoney.cpp:22 : `newCurrency' undeclared (first use this function)
ExtMoney.cpp: In method `void ExtMoney::Initi alize(long int, long int,
basic_string<ch ar,string_char_ traits<char>,__ default_alloc_t emplate<true,0>
)':

MoneyType.h:15: `long int MoneyType::doll ars' is private
ExtMoney.cpp:35 : within this context
MoneyType.h:16: `long int MoneyType::cent s' is private
ExtMoney.cpp:36 : within this context
------------------------------------------------------------------
Base class defination
------------------------------------------------------------------
#ifndef MONEY_TYPE_H
#define MONEY_TYPE_H

class MoneyType{
public:
MoneyType();
MoneyType(long newDollars, long newCents):dolla rs(newDollars),
cents(newCents) {};
void Initialize(long , long);
long DollarsAre() const;
long CentsAre() const;
void Print() const;
MoneyType Add(const MoneyType &) const;

private:
long dollars;
long cents;
};

#endif
------------------------------------------------------------------
Base class implementation
------------------------------------------------------------------
#include <iostream>
#include "MoneyType. h"
using namespace std;

// default constructor
MoneyType::Mone yType(){
dollars = 0;
cents = 0;
}
/*
// other constructor
MoneyType::Mone yType(long newDollars, long newCents){
dollars = newDollars;
cents = newCents;
}
*/
// dollars is set to newDollars; cents is set to newCents.
void MoneyType::Init ialize(long newDollars,long newCents){
dollars = newDollars;
cents = newCents;
}

// Class member dollars is returned.
long MoneyType::Doll arsAre() const{
return dollars;
}

// Class member Cents is returned.
long MoneyType::Cent sAre() const{
return cents;
}

// print the member dollars and cents.
void MoneyType::Prin t() const{
cout << dollars << " " << cents << " ";
}

// value + self is returned.
MoneyType MoneyType::Add( const MoneyType &value) const{
MoneyType result;
result.cents = cents+value.cen ts;
result.dollars = dollars+value.d ollars;
return result;
}
------------------------------------------------------------------
derived class defination
------------------------------------------------------------------
#ifndef EXTMONEY_H
#define EXTMONEY_H

#include <string>
#include "MoneyType. h"
using namespace std;

class ExtMoney: public MoneyType{
public:
// - default constructor
// - currency is set to "dollars"
ExtMoney();

// parameters: long newDollars, long newCents, const string newCurrency
// - initialize dollars and cents to newDollars and newCents
respectively
// using initialization list
// - currency is set to newCurrency
ExtMoney(long, long, const string);

// - print the member dollars, cents and currency
void Print() const;

// - class member currency is returned
string CurrencyIs() const;

// parameters: long newDollars, long newCents, string newCurrency
// - dollars is set to newDollars
// - cents is set to newCents,
// - currency is set to newCurrency
void Initialize(long , long, string);

private:
string currency;
};

#endif

------------------------------------------------------------------
dervied class implementation
------------------------------------------------------------------
#include <iostream>
#include "ExtMoney.h "
using namespace std;

// - default constructor
// - currency is set to "dollars"
ExtMoney::ExtMo ney(){
currency = "";
}

// parameters: long newDollars, long newCents, const string newCurrency
// - initialize dollars and cents to newDollars and newCents
respectively
// using initialization list
// - currency is set to newCurrency
ExtMoney::ExtMo ney(long newDollars, long newCents, const string
newCurrency)
:MoneyType(newD ollars, newCents), currency(newCur rency){
currency = newCurrency;
}

// - print the member dollars, cents and currency
void ExtMoney::Print () const{
cout << newDollars << " " << newCents << " " << newCurrency;
}

// - class member currency is returned
string ExtMoney::Curre ncyIs() const{
return(currency );
}

// parameters: long newDollars, long newCents, string newCurrency
// - dollars is set to newDollars
// - cents is set to newCents,
// - currency is set to newCurrency
void ExtMoney::Initi alize(long newDollars, long newCents, string
newCurrency){
dollars = newDollars;
cents = newCents;
currency = newCurrency;
}
------------------------------------------------------------------
main class
------------------------------------------------------------------
#include <iostream>
#include "ExtMoney.h "
using namespace std;

int main(){
MoneyType money;
cout << "initilaize d by default constructors" << endl;
money.Print(); //0 , 0
cout << endl;

ExtMoney extMoney1;
extMoney1.Print ();
cout << endl;

cout << "initialize d by other constructors" << endl;
ExtMoney extMoney2(3000, 88, "forints");
extMoney2.Print ();
cout << endl;

cout << "initialize d at run time" << endl;
extMoney1.Initi alize(5000, 99, "pounds");
extMoney1.Print ();
cout << endl;
return 0;
}

------------------------------------------------------------------

Nov 8 '05 #1
4 1912
Mr*******@gmail .com wrote:
How I can fix this problem?
I don't know why it alway prompted (first use this function). I have
use newDollars, newCents... to access the base class member variable.
Why? 'newDollars' is an argument in another function. It has no relation
to the 'Print' function.
And the member functions for Initialize() and Print(), is it function
overloading? it already have this functions in the base class.


Start by understanding what you're allowed to access where. What is the
difference between a member variable and a local variable? Open your C++
book and give it another read.

V
Nov 8 '05 #2
I get what you mean. Let me try!

Thx a lot,

Nov 8 '05 #3
Is that using get set function in the base class public?

e.g.
MoneyType(long newDollars, long newCents):dolla rs(newDollars),
cents(newCents) {};
void set_dollars = (long& newDollars);
void set_cents = (long& newCents);
long get_dollars();
long get_cents();

Nov 8 '05 #4
Amazing wrote:
Is that using get set function in the base class public?

e.g.
MoneyType(long newDollars, long newCents):dolla rs(newDollars),
cents(newCents) {};
void set_dollars = (long& newDollars);
void set_cents = (long& newCents);
long get_dollars();
long get_cents();


I honestly have no idea what you're talking about.

V
Nov 8 '05 #5

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

Similar topics

2
4369
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
22
23347
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 examples?
6
2091
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
1
1773
by: relient | last post by:
Hi, I just started the chapter on "Inheritance" and I'm a bit confused so I have three questions: first, here's the code: using System; using System.Collections.Generic; using System.Text; namespace ConsoleProject1 { public class Base
5
2750
by: colint | last post by:
Hi I'm fairly new to c++ and I have a question regarding inheritance. I'm trying to create a class based on 2 inherited classes, e.g. class A { ... } class B: public A
0
1201
by: mflll | last post by:
I want to derive or substitute an Item element containing just text by another element with regular contents. The context is that I have a contract which has Block's that represent clause's. Each Blcok has one or more units called Items. Consider these as paragraphs or sentences within the clause. This works fine. The Item contains simple text and has no child elements.
13
2033
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python folks might find to taste. Here's the code - first the 'engine', then some code demonstrating the usage patterns. For me (and maybe for some of you), it promotes readability and some
3
1826
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
26
5354
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
3
2896
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table: tlkpColor (PK) tlkpColorID
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.