I'm having problem with the function DisplayValues() in this program. First, I'm asked to write the prototype only for the function inside the class. Second requirement is to place the entire function DisplayValues() outside the class. DisplayValues() is a const inline member function.
All the above needs to be in a header file. I've written below and got errors "'accountType' not declared in this scope" for the DisplayValues() function. I did search for solutions but none worked without modifying the requirements above. Would anyone please advise?
My codes:
File SavingsAccount.h
ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
class SavingsAccount
{
private:
int accountType;
public:
inline void DisplayValues() const;
};
inline void DisplayValues()
{
cout << "Account type: " << accountType << '\n'
}
#endif //SAVINGSACCOUNT_H
File SavingsAccount.cpp
#include <iostream>
#include "SavingsAccount.h"
using namespace std;
void SavingsAccount::GetInitialValues()
{
cout << "Enter account type:\n";
cin >> accountType;
}
File main.cpp
#include <iostream>
#include "SavingsAccount.h"
using namespace std;
int main()
{
SavingsAccount ac;
ac.GetInitialValues();
ac.DisplayValues();
return 0;
}
Your code has 3 obvious problems that I can see:
This is a free function, not a class member function.
inline void DisplayValues()
{
cout << "Account type: " << accountType << '\n'
}
Fix:
inline void SavingsAccount::DisplayValues()
{
cout << "Account type: " << accountType << '\n'
}
Your declaration is inline void DisplayValues() const; but definition is missing the const keyword.
inline void SavingsAccount::DisplayValues()
^^^^^ const missing here
You are missing a semicolon is this line:
cout << "Account type: " << accountType << '\n'
^^ semicolon missing here
Related
Can anyone tell me why i get the error "name was not declared in the scope when running this?
Thanks.
class lrn11_class{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string lrn11_name;
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
return 0;
}
This should work - see comments (BTW use std:: - Why is "using namespace std" considered bad practice?)
#include <iostream>
#include <string>
class lrn11_class{
public:
void setName(const std::string& x){ // Potentially saves copying overhead
name = x;
}
std::string getName() const { // Look up const and its uses
return name;
}
private:
std::string name; // - Used: string lrn11_name; but functions use name!
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setName("Zee"); // Fixed typo
std::cout << lrn11_nameobject.getName() << std::endl; // Ditto
return 0;
}
You have declare lrn11_name as a member varible for this class. But in set and get functions you are using name.
Other than than you need to call functions as you have defined.
so instead of :-
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
You have to use following code :-
lrn11_nameobject.setName("Zee");
cout << lrn11_nameobject.getName() << endl;
Make sure that
#include <iostream>
using namespace std;
should be included.
I am trying to make a simple friend function work, but not in just one source file. I seem to get an error and I can't seem to find an answer why.
Please have a look at my code:
----------classOne.h--------------
#ifndef CLASSONE_H_
#define CLASSONE_H_
using namespace std;
class ClassOne {
private:
int m_a;
int m_b;
public:
ClassOne(int a, int b);
void printValuesOne();
friend void ClassTwo::twoPrintsOne();
};
-
----------classOne.cpp------------
#include <iostream>
#include "classOne.h"
using namespace std;
ClassOne::ClassOne(int a, int b) {
m_a = a;
m_b = b;
}
void ClassOne::printValuesOne() {
cout << "m_a: " << m_a << " " << "m_b: " << m_b << endl;
}
.
----------classTwo.h-------------
#ifndef CLASSTWO_H_
#define CLASSTWO_H_
using namespace std;
class ClassTwo {
private:
int m_c;
int m_d;
public:
ClassTwo(int c, int d);
void printValuesTwo();
twoPrintsOne();
};
#endif
-
---------classTwo.cpp-----------
#include <iostream>
#include "classTwo.h"
using namespace std;
ClassTwo::ClassTwo(int c, int d) {
m_c = c;
`enter code here`m_d = d;
}
void ClassTwo::printValuesTwo() {
cout << "m_c: " << m_c << " " << "m_d: " << m_d << endl;
}
void twoPrintsOne() {
cout << "ClassTwo: " << m_a: " << m_a << " " << "m_b: " << m_b << endl;
}
Basically ClassOne and ClassTwo are the same sort of thing, but only one of ClassTwo's method has access to all of ClassOne's members, so ClassTwo can print ClassOne's member variables. However, when I try to compile the whole program (I haven't provided the main method here), I get this error an error:
classOne.h:19:15: error: ‘ClassTwo’ has not been declared
friend void ClassTwo::twoPrintsOne();
^
Can someone help and explain?
ClassOne doesn't know about ClassTwo, therefore it cannot befriend with any of its methods. You have to add:
#include "classTwo.h"
on top of your classOne.h.
You are getting confused with what "friend" is supposed to do.
"friending" ClassTwo will allow ClassTwo to access the private members of ClassOne as if they were public.
It will not link those members between the two classes in any way.
You could have ClassTwo inherit from ClassOne, or add a ClassOne member in ClassTwo
I have a fairly simple C++ code that doesn't seem to be compiling properly. Essentially, I have some globally defined functions declared in my GLOBAL.HPP file, and are defined in my GLOBAL.CPP file. Then I have a class, EuroOption, that consists of a struct datamember. The class EuroOption has its own member functions that essentially do the same exact thing that the global functions do--so I defined them similarly, and just called global functions inside of the EuroOption member function definitions. Please see below:
//
//GLOBAL.HPP
//
#ifndef GLOBAL_HPP
#define GLOBAL_HPP
#include <iostream>
#include <math.h>
#include <boost/math/distributions/normal.hpp>
#include <boost/math/distributions.hpp> // For non-member functions of distributions
using namespace std;
//using namespace boost::math;
namespace GLOBAL // Encapsulate Point in the Global namespace
{
struct EuroOptionData
{
double r; // Interest rate
double sig; // Volatility
double K; // Strike price
double T; // Expiry date
double b; // Cost of carry
};
double n(double x);
double N(double x);
double CallPrice(EuroOptionData od, double S);
double PutPrice(EuroOptionData od, double S);
double PutParity(EuroOptionData od, double S);
double CallParity (EuroOptionData od, double S);
} // Close namespace GLOBAL
#endif
Here is the EuroOption.HPP file:
//
//
//
#ifndef EUROOPTION_HPP
#define EUROOPTION_HPP
#include <string>
#include "Global.hpp"
using namespace std;
using namespace GLOBAL;
class EuroOption
{
private:
public:
struct EuroOptionData od;
//EuroOption class functions
EuroOption(); // Default call option
EuroOption(const EuroOption& option2); // Copy constructor
virtual ~EuroOption(); //Destructor
//EuroOption Global Function Calls
double EuroCallPrice(EuroOptionData od, double S);
double EuroPutPrice(EuroOptionData od, double S);
double EuroCallParity(EuroOptionData od, double S);
double EuroPutParity(EuroOptionData od, double S);
//EuroOption class operators
EuroOption& operator = (const EuroOption& option2); //Assignment Operator
};
#endif
And a snippet of the EuroOption.CPP file:
//
//
//
#include "EuroOption.hpp"
#include <cmath>
#include <iostream>
using namespace GLOBAL;
{
double EuroOption::EuroCallPrice(EuroOptionData od, double S)
{
return CallPrice(od,S);
};
double EuroOption::EuroPutPrice(EuroOptionData od, double S)
{
return CallPrice(od,S);
};
.....
...
}
And finally, a snippet of my Test.CPP file where I test functionality:
//
//
//
#include "Global.hpp"
#include "EuroOption.hpp"
#include <iostream>
using namespace GLOBAL;
int main()
{
EuroOption Batch1; //Initialize EuroOption class object Batch1
cout << "S1: "; double S1; cin >> S1;
cout << "Stock Call Option: " << EuroCallPrice(Batch1.od, S1) << endl;
cout << "Stock Put Option: " << EuroPutPrice(Batch1.od, S1) <<endl;
cout << "Put Call Parity - Call Option:"<< EuroCallParity(Batch1.od, S1)<<endl;
cout << "Put Call Parity - Put Option: "<< EuroPutParity(Batch1.od, S1)<<endl;
//****None of these functions compile. They all state "identifier EuroCallPrice (..etc.) is undefined."
cout << "S1: "; double S1; cin >> S1;
cout << "Stock Call Option: " << CallPrice(Batch1.od, S1) << endl;
cout << "Stock Put Option: " << PutPrice(Batch1.od, S1) <<endl;
cout << "Put Call Parity - Call Option:"<< CallParity(Batch1.od, S1)<<endl;
cout << "Put Call Parity - Put Option: "<< PutParity(Batch1.od, S1)<<endl;
//****These functions all compile properly. They are the original global functions.
I realize this is a lot of code to sift through, but any ideas would be greatly appreciated. As noted in the above code, the original global functions work perfectly, but I want to use the class EuroOption function to call that global function.
Many thanks!
Silly me! All i needed to do was call the EuroCallPrice...etc functions on Batch1.
Thanks for all your help!
header file:
#ifndef H_bankAccount;
#define H_bankAccount;
class bankAccount
{
public:
string getAcctOwnersName() const;
int getAcctNum() const;
double getBalance() const;
virtual void print() const;
void setAcctOwnersName(string);
void setAcctNum(int);
void setBalance(double);
virtual void deposit(double)=0;
virtual void withdraw(double)=0;
virtual void getMonthlyStatement()=0;
virtual void writeCheck() = 0;
private:
string acctOwnersName;
int acctNum;
double acctBalance;
};
#endif
cpp file:
#include "bankAccount.h"
#include <string>
#include <iostream>
using std::string;
string bankAccount::getAcctOwnersName() const
{
return acctOwnersName;
}
int bankAccount::getAcctNum() const
{
return acctNum;
}
double bankAccount::getBalance() const
{
return acctBalance;
}
void bankAccount::setAcctOwnersName(string name)
{
acctOwnersName=name;
}
void bankAccount::setAcctNum(int num)
{
acctNum=num;
}
void bankAccount::setBalance(double b)
{
acctBalance=b;
}
void bankAccount::print() const
{
std::cout << "Name on Account: " << getAcctOwnersName() << std::endl;
std::cout << "Account Id: " << getAcctNum() << std::endl;
std::cout << "Balance: " << getBalance() << std::endl;
}
Please help i get an error under getAcctOwnersName, and setAcctOwnersName stating that the declaration is incompatible with "< error-type > bankAccount::getAcctOwnersName() const".
You need to
#include <string>
in your bankAccount header file, and refer to the strings as std::string.
#ifndef H_bankAccount;
#define H_bankAccount;
#include <string>
class bankAccount
{
public:
std::string getAcctOwnersName() const;
....
once it is included in the header, you no longer need to include it in the implementation file.
I've found that when a private member variable and a member function have the same name the IDE gives me the "incompatible" error, perhaps that is what you are experiencing...
Sometimes this error occur because it's vary from machine to machine. Your program will work fine if you declare your class and all of its implementations in one file instead doing declaration of class in other file and linked it with your driver file.
Again: This is totally machine dependent error.
In visual studio 2012 you will face this kind of error because it not work for these files while in other versions of vs you will not face any error type exception.
Hope it's worth.....
I'm studying for pointer and inheritance of C++.\
I made pointer of vector Employee class which has name and salary in it.
Also, it has print function which printout name and salary.
I made it virtual
virtual void print() const;
Also here's the implementation of function
void Employee::print() const
{
cout << "Inquiry Employee info..." << endl;
cout << "Name:" << get_name() << "\n";
cout << "Salary:" << get_salary() << "\n" << "\n";
}
In derived class, I added one more private data Department Name and override print function.
I put derived class in pointer vector, and tried to call the function on derived function.
However, it only calls the function in base class.
When I make derived class object itself and called that print function, it worked.
What should I change to make derived class object in base class pointer vector can call function in derived class?
Addition ##
#ifndef MANAGER_H
#define MANAGER_H
#include <string>
#include<iostream>
#include "ccc_empl.h"
#include <iomanip>
class Manager : public Employee
{
public:
Manager();
Manager(string name, double salary, string dept);
~Manager();
virtual string get_department() const;
void print();
private:
string deptName;
};
#endif
implementation
#include <iostream>
#include <string>
#include "manager.h"
#include "ccc_empl.h"
Manager::Manager(){}
Manager::Manager(string name, double salary, string dept)
: Employee(name,salary)
{
deptName = dept;
}
Manager::~Manager(){}
string Manager::get_department() const
{
return deptName;
}
void Manager::print()
{
cout << "Inquiry Manager info..." << endl;
cout << "Name:" << get_name() << "\n";
cout << "Salary:";
cout << get_salary() << "\n";
cout << "Department:"<< get_department() << endl << endl;
}
You haven't shown us the Derived class print function, but I would guess that you forgot to declare it const. Easy mistake to make.