M
MrTang001
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:rint() 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::Initialize(long int, long int,
ExtMoney.cpp:35: within this context
MoneyType.h:16: `long int MoneyType::cents' 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):dollars(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::MoneyType(){
dollars = 0;
cents = 0;
}
/*
// other constructor
MoneyType::MoneyType(long newDollars, long newCents){
dollars = newDollars;
cents = newCents;
}
*/
// dollars is set to newDollars; cents is set to newCents.
void MoneyType::Initialize(long newDollars,long newCents){
dollars = newDollars;
cents = newCents;
}
// Class member dollars is returned.
long MoneyType:ollarsAre() const{
return dollars;
}
// Class member Cents is returned.
long MoneyType::CentsAre() const{
return cents;
}
// print the member dollars and cents.
void MoneyType:rint() const{
cout << dollars << " " << cents << " ";
}
// value + self is returned.
MoneyType MoneyType::Add(const MoneyType &value) const{
MoneyType result;
result.cents = cents+value.cents;
result.dollars = dollars+value.dollars;
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::ExtMoney(){
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::ExtMoney(long newDollars, long newCents, const string
newCurrency)
:MoneyType(newDollars, newCents), currency(newCurrency){
currency = newCurrency;
}
// - print the member dollars, cents and currency
void ExtMoney:rint() const{
cout << newDollars << " " << newCents << " " << newCurrency;
}
// - class member currency is returned
string ExtMoney::CurrencyIs() 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::Initialize(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 << "initilaized by default constructors" << endl;
money.Print(); //0 , 0
cout << endl;
ExtMoney extMoney1;
extMoney1.Print();
cout << endl;
cout << "initialized by other constructors" << endl;
ExtMoney extMoney2(3000, 88, "forints");
extMoney2.Print();
cout << endl;
cout << "initialized at run time" << endl;
extMoney1.Initialize(5000, 99, "pounds");
extMoney1.Print();
cout << endl;
return 0;
}
------------------------------------------------------------------
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:rint() 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::Initialize(long int, long int,
MoneyType.h:15: `long int MoneyType::dollars' is privatebasic_string said:
ExtMoney.cpp:35: within this context
MoneyType.h:16: `long int MoneyType::cents' 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):dollars(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::MoneyType(){
dollars = 0;
cents = 0;
}
/*
// other constructor
MoneyType::MoneyType(long newDollars, long newCents){
dollars = newDollars;
cents = newCents;
}
*/
// dollars is set to newDollars; cents is set to newCents.
void MoneyType::Initialize(long newDollars,long newCents){
dollars = newDollars;
cents = newCents;
}
// Class member dollars is returned.
long MoneyType:ollarsAre() const{
return dollars;
}
// Class member Cents is returned.
long MoneyType::CentsAre() const{
return cents;
}
// print the member dollars and cents.
void MoneyType:rint() const{
cout << dollars << " " << cents << " ";
}
// value + self is returned.
MoneyType MoneyType::Add(const MoneyType &value) const{
MoneyType result;
result.cents = cents+value.cents;
result.dollars = dollars+value.dollars;
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::ExtMoney(){
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::ExtMoney(long newDollars, long newCents, const string
newCurrency)
:MoneyType(newDollars, newCents), currency(newCurrency){
currency = newCurrency;
}
// - print the member dollars, cents and currency
void ExtMoney:rint() const{
cout << newDollars << " " << newCents << " " << newCurrency;
}
// - class member currency is returned
string ExtMoney::CurrencyIs() 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::Initialize(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 << "initilaized by default constructors" << endl;
money.Print(); //0 , 0
cout << endl;
ExtMoney extMoney1;
extMoney1.Print();
cout << endl;
cout << "initialized by other constructors" << endl;
ExtMoney extMoney2(3000, 88, "forints");
extMoney2.Print();
cout << endl;
cout << "initialized at run time" << endl;
extMoney1.Initialize(5000, 99, "pounds");
extMoney1.Print();
cout << endl;
return 0;
}
------------------------------------------------------------------