I know that private class data is only accessible within the class; however, the examples that I have read show similar use of private members in their program code. I am attempting to use functions to access and manipulate the private class members, but it is not working. What am I doing incorrectly? I have tried substituting the Data.SelectionF() function for Data.selection after the first cin and for all instances of the use of the selection member variables without success. I also tried the same approach for all instances of the value member variable. Thanks
#include <iostream>
#include <iomanip>
using namespace std;
class allData {
private:
char selection;
double r;
double centimeter;
double value;
public:
double ConvertC (double value);
double ConvertR (double value);
double valueF (double value);
char selectionF (char selection);
allData Data ();
} Data;
int main() {
cout << "Enter C for converting your Feet to Centimeters.\n"
"Enter R for converting your Inches to Centimeters.\n";
cin >> Data.selection;
cout << "\nYou selected to convert to: " <<
Data.selectionF(Data.selection) << ".\n\n";
cout << "Enter your starting value to two decimal places, and press
ENTER.\n\n";
cin >> Data.value;
cout << "\nYou entered a starting value of: " <<
Data.valueF(Data.value) << ".\n\n";
//switch to decide which conversion function to use from the structure
switch (Data.selectionF(Data.selection)) {
case 'c': { Data.ConvertC(Data.value);
cout << "Your Feet converted to Centimeters is: " <<
Data.ConvertC(Data.value) << "\n\n";
break;
}
case 'C': { Data.ConvertC(Data.value);
cout << "Your Feet converted to Centimeters is: " <<
Data.ConvertC(Data.value) << "\n\n";
break;
}
case 'r': { Data.ConvertR(Data.value);
cout << "Your Inches converted to Centimeters is: " <<
Data.ConvertR(Data.value) << "\n\n";
break;
}
case 'R': { Data.ConvertR(Data.value);
cout << "Your Inches converted to Centimeters is: " <<
Data.ConvertR(Data.value) << "\n\n";
break;
}
default: {cout << "You entered an invalid selection for your conversion"
"choice.\n";
break;
}
}
return 0;
}
//Function definitions
double allData::ConvertC (double value) {
centimeter = value * 30.48;
return centimeter;
}
double allData::ConvertR (double value) {
r = value * 2.54;
return r;
}
double allData::valueF (double value) {
return value;
}
char allData::selectionF (char selection) {
return selection;
}
//End of program.
Whatever examples you've read, either they are wrong or you misunderstood them. You cannot access private class members outside of the class (barring friend declarations, of course). That's what a private class member means, by definition.
cin >> Data.selection;
selection is a private class member. It cannot be accessed from your main(). That's, pretty much, all that can be said about it.
If I did not misunderstand your question let me tell you something about your problem.
Firstly, with this line "cin >> Data.value;" you are trying to get the private value of the "DATA" class in fact this is wrong. You cannot access to the private variable from the outside of the "DATA" class. The object "DATA" is not inside the "DATA" class. On the other hand if you want to access the private variables inside the class indirectly, then you can write a function like that:
public:
void function_name(const DATA &dataObject) const
{
cout << "The selection value : " << dataObject.selection << endl;
}
This part of code will access to the private variables inside the class indirectly. Remember if a function is inside the class then it can access all the private variables and there is no something wrong with this code.
Related
I'm guessing I might have to use pointers, but haven't gone in depth too much on them in class yet to try and implement them in my program. I have this so far, the printing function is towards the middle of the program. I'm not quite sure on how to print out the elements from the vector as my approach didn't work.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class rolodex
{
string name;
string street, town, state;
string zip;
string phone;
vector <rolodex> entries;
public:
rolodex();
void getmenu();
void add_entry();
void set_name();
void set_address();
void set_phone();
void printinfo();
};
rolodex :: rolodex() : name(""), street(""), town(""), state(""), zip(""),
phone(""), entries()
{
}
void rolodex :: getmenu()
{
cout << "\n\n1)Add Entry";
cout << "\n5)Print All Entries";
cout << "\n6)Exit" << endl;
}
void rolodex :: add_entry()
{
rolodex temp;
cout << "\n\nEnter Name: ";
temp.set_name();
temp.set_address();
cout << "\n\nEnter Your Phone Number: ";
temp.set_phone();
entries.push_back(temp);
}
void rolodex :: set_name()
{
cin.ignore();
getline(cin, name);
}
void rolodex :: set_address()
{
cout << "\n\nNow we'll enter address information.";
cout << "\n\nStreet: ";
getline(cin, street);
cout << "\n\nTown: ";
getline(cin, town);
cout << "\n\nState: ";
getline(cin, state);
cout << "\n\nZip: ";
getline(cin, zip);
}
void rolodex :: set_phone()
{
getline(cin, phone);
}
void rolodex :: printinfo()
{
for(unsigned int i = 0; i < entries.size(); i++)
{
cout << entries[i] << endl; //This is where I'm stuck since I've only
//worked with vectors of non-object data
//type
}
}
int main()
{
rolodex person, menu;
short choice;
bool done = false;
do
{
menu.getmenu();
cout << "\n\nEnter a choice: ";
cin >> choice;
switch(choice)
{
case 1:
person.add_entry();
break;
case 5:
person.printinfo();
break;
case 6:
done = true;
break;
default:
cout << "\n\nInvalid Entry." << endl << endl;
}
} while(!done && isdigit(choice));
return 0;
}
πάντα ῥεῖ is right, but to add a little more detail...
You need to specify how you want the stream to handle your object. This is done by by adding a << operator. For example:
std::ostream& operator<<(std::ostream& s, const rolodex& r){
// Or however you want to format it.
s << "Name: " << r.name << " : ";
s << "Street: " << r.street << " : ";
s << "Town: " << r.town << " : ";
s << "State: " << r.state << " : ";
s << "Zip: " << r.zip << "\n";
}
Unfortunately, the function above tries to access the private fields of your class, which it can't because it is not part of the class definition.
An easy way to address that is to declare this function a "friend" inside of the class definition, like such:
friend std::ostream& operator<<(std::ostream&, const rolodex&);
...And since you might appreciate it, one big copy-pasteable chunk that you can use directly that should make your function work:
class rolodex
{
string name;
string street, town, state;
string zip;
string phone;
vector <rolodex> entries;
public:
rolodex();
void getmenu();
void add_entry();
void set_name();
void set_address();
void set_phone();
void printinfo();
friend std::ostream& operator<<(std::ostream&, const rolodex&);
};
std::ostream& operator<<(std::ostream& s, const rolodex& r){
// Or however you want to format it.
s << "Name: " << r.name << " : ";
s << "Street: " << r.street << " : ";
s << "Town: " << r.town << " : ";
s << "State: " << r.state << " : ";
s << "Zip: " << r.zip << "\n";
}
Following up on πάντα ῥεῖ's suggestion, here's one way of doing that, changing your design as little as possible:
1) Create a non-member overloaded operator<< for your rolodex class:
std::ostream& operator<< (std::ostream& os, const rolodex& rol)
{
os << rol.name << ":" << std::endl
<< "\t" << rol.street << std::endl
<< "\t" << rol.town << std::endl
<< "\t" << rol.state << std::endl
<< "\t" << rol.zip << std::endl
<< "\t" << rol.phone << std::endl;
return os;
}
.. but the compiler will chide you for attempting to access private members (by default, members are private) from outside the class, so you would have to relax the rules a bit:
class rolodex
{
...
public:
...
friend std::ostream& operator<< (std::ostream& os, const rolodex& rol);
};
You can't have the operator<< inside the class itself, see does-overloading-operator-works-inside-the-class.
However, it is almost always better design to add getter functions to your public interface anyway. You would have get_name() etc in the public: section of your class def, those functions would initially just return the values of the private member variables, and then your operator<< can use them instead of trying to access the private members. You then no longer require the friend declaration.
I upvoted Some programmer dude's remark about your design
The code for letting the use input the data really shouldn't be inside the rolodex class, because it makes the class hard to reuse. Image wanting to re-use the rolodex from a graphical interface, for example, and it's not such a good idea to have the rolodex contain instances of itself inside the vector.
I would suggest a
1) Person class containing all the person's attributes, with public getters get_name() and setters set_name() that don't use a specific entry method, just take the data as arguments e.g. set_name(std::string& name).
2) an non-member operator<< to output a person to an output stream
3) a Rolodex class with a private std::vector<Person> and methods to add a person, write all the persons to an output stream, etc..
Good luck & enjoy :-)
Edit: the menu structure on the terminal should IMHO be left inside the main() function or encapsulated into another class. But certainly don't leave it in Rolodex or worse, Person.
So, my assignment in class was to make a C++ program that essentially makes a database with a number of options (adding to it, deleting entries, modifying, searching and listing). It has to be done specifically with arrays, not vectors or classes or whatever.
I decided to make a number of functions to handle each option, and have them all call each other. After extensive googling, I also decided to let a struct handle the declarations so I can use them in all functions without using :: marks. I specifically made everything depend on each other because the teacher hinted that we're going to have to do further work with it, so if I modify something, everything else changes to accommodate.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
struct va{
public:
int i, j, k, l; //for possible loops or variables I only need for a very short time
int id = 0;
int name = id+1;
// like 6 other ints I also declared here, including year2
int achi = year2+1;
//The above is for easier identification of pro[whatever][needed data]. The +1 method is to allow for easier editing later.
int size = 20, row = 0; //This is important for addition
string searchterm = ""; //this is for searching
public:
int main();
void MainMenu();
void Addition();
void Deletion();
void Search();
void Modify();
void List();
};
void MainMenu();
void Addition();
void Deletion();
void Search();
void Modify();
void List();
//I just find it neater to make side functions after the main one.
int main()
{
setlocale(LC_ALL, "");
const int column = achi;
const int initsize = size; //These two are so I can edit the size of the array from the struct directly
string pro[initsize][column]; //This creates the array that's the actual database
cout << endl << "Welcome to the League of Legends Pro Players database!" << endl << endl;
cout << endl << "Please, use the menu to access its functions.";
MainMenu();
cout << endl;
return 0;
}
void MainMenu()
{
cout << endl << "Main Menu" << endl;
cout << endl << "1: add an entry to the database.";
cout << endl << "2: delete an existing entry from the database.";
cout << endl << "3: search for an existing entry in the database.";
cout << endl << "4: modify an existing entry.";
cout << endl << "5: list all existing entries." << endl;
cin >> i;
switch(i)
{
case 1: Addition();
case 2: Deletion();
case 3: Search();
case 4: Modify();
case 5: List();
}
}
(I haven't written the actual functions for options yet.) However, when I tried to run it, I was told 'achi' wasn't declared in main, although I made everything public just so I won't run into this error. How could I make main "see" the struct and its variables?
You have only defined a type, you don't have an values of that type. You have also declared but not defined a number of member functions, and then declared (and presumably defined, tho many are not shown) free functions with the same names.
When providing out-of-class definitions of member functions of struct va, you need to use va:: to qualify the names of the members, to distinguish them from anything else with the same name. If this were not the case, then all the good names would be used up by members of classes in the standard library.
It's also good practice to declare variables at the narrowest possible location. Don't clutter the data members of va with things that can be local to it's member functions.
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
struct va {
static constexpr int size = 20;
static constexpr int column = ???;
std::string pro[size][column];
void MainMenu();
void Addition();
void Deletion();
void Search();
void Modify();
void List();
};
int main()
{
setlocale(LC_ALL, "");
va instance;
cout << endl << "Welcome to the League of Legends Pro Players database!" << endl << endl;
cout << endl << "Please, use the menu to access its functions.";
instance.MainMenu();
cout << endl;
return 0;
}
void va::MainMenu()
{
cout << endl << "Main Menu" << endl;
cout << endl << "1: add an entry to the database.";
cout << endl << "2: delete an existing entry from the database.";
cout << endl << "3: search for an existing entry in the database.";
cout << endl << "4: modify an existing entry.";
cout << endl << "5: list all existing entries." << endl;
int i;
cin >> i;
switch(i)
{
case 1: Addition();
case 2: Deletion();
case 3: Search();
case 4: Modify();
case 5: List();
}
}
I am in a beginner C++ course. Here are the instructions for my assignment:
Write a program that calls a value-returning function that prompts the user to enter the weight of a person in pounds and then calls another value returning function to calculate the equivalent weight in kilograms. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
I thought everything was perfect. However, I am a getting a debug error, which is Runtime Check Error #3 - T. Please review my code and tell me what is wrong here. Remember, I am a beginner. Thank you.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
string get_date();
void student_heading();
float get_user_input(float);
float convert_weight(float);
int main()
{
cout << fixed << showpoint << setprecision(2);
string mydate;
float weight_lbs;
float weight_kgs;
mydate = get_date();
student_heading();
weight_lbs = get_user_input();
weight_kgs = convert_weight(weight_lbs);
return 0;
}
string get_date()
{
string mydate;
cout << "Enter today's date:";
getline(cin, mydate);
return mydate;
}
void student_heading()
{
cout << "*******************" << endl;
cout << "Student" << endl;
cout << "ID Number" << endl;
cout << "SYCS-135" << endl;
cout << "Assignment 6" << endl;
cout << "October 6, 2015" << endl;
cout << "******************" << endl;
}
float get_user_input(float lb_weight)
{
cout << "Please provide us with your weight, in pounds:";
cin >> lb_weight;
return lb_weight;
}
float convert_weight(float kg_weight)
{
float lb_weight;
kg_weight = lb_weight / 2.2;
return kg_weight;
}
Your error is that you are calling weight_lbs = get_user_input();
You cant call get_user_input with no argument because you do not have any function like that.
Your code has several issues.
Your function prototypes are not correct. The parameter in the braces have to contain the variable name as well as data type. So only function(int) is not enaugh, it should be function(int MyVariable). So the error was caused by calling function, which didn't exist, because your function prototype was not correct (expected a parameter).
Your get_user_input() function has a parameter which is not needed. The whole function should look like this:
float get_user_input()
{
cout << "Please provide us with your weight, in pounds:";
float lb_weight;
cin >> lb_weight;
return lb_weight;
}
Function for mass conversion can look like this:
float convert_weight(float lb_weight)
{
return lb_weight / 2.2;
}
Your main function then should look like this:
int main()
{
cout << fixed << showpoint << setprecision(2);
string mydate;
float weight_lbs;
float weight_kgs;
mydate = get_date();
cout << mydate << endl;
student_heading();
weight_lbs = get_user_input();
weight_kgs = convert_weight(weight_lbs);
cout << weight_kgs << "Kg";
getchar();
getchar();
return 0;
}
Don't forget to change the function prototypes to:
float get_user_input();
float convert_weight(float lb_weight);
I'm currently having some problems with my class and the member functions, particularly with taking the users input from the setter functions and then displaying that info from the print info function. I have a separate functions that allow the user to enter information about the character and then a getter function to then use in printing out the info that the user entered. When I first ran it I had an error about needing to use a pointer to a member function and I added the &Character::GetCharacterName and the others in my print info function. Now when I run the program through my main function (I didn't include it because it simply calls all the functions) my program will run but all the values are set at 1 no matter what the user entered. I know it has something to do with pointers so any help with correctly setting this up so that it returns the values that the user entered would be appreciated. Thanks
Character.h file
class Character
{
public:
Character();
void SetCharacterName();
void SetCharacterType();
void SetCharacterLevel();
string GetCharacterName();
string GetCharacterType();
double GetCharacterLevel();
void PrintInfo();
private:
string CharacterName;
string CharacterType;
double CharacterLevel;
};
Character.cpp file
Character::Character()
{
CharacterLevel = 1.0;
}
void Character::SetCharacterName()
{
cout << "\nWhat is the character's name? ";
cin >> CharacterName;
}
void Character::SetCharacterType()
{
cout << "\nWhat is the character's type? ";
cin >> CharacterType;
}
void Character::SetCharacterLevel()
{
cout << "\nWhat is the character's level? ";
cin >> CharacterLevel;
}
string Character::GetCharacterName()
{
return CharacterName;
}
string Character::GetCharacterType()
{
return CharacterType;
}
double Character::GetCharacterLevel()
{
return CharacterLevel;
}
void Character::PrintInfo()
{
system("pause");
system("cls");
cout << "\nCharacter name is " << &Character::GetCharacterName << ".\n";
cout << "\nCharacter type is " << &Character::GetCharacterType << ".\n";
cout << "\nCharacter level is " << &Character::GetCharacterLevel << ".\n";
}
Use (), to do a method call in PrintInfo:
cout << "\nCharacter name is " << GetCharacterName() << ".\n";
etc.
I sugest :
this->GetCharacterName();
In the print method.
Why does the depositmoney() method within the operation method of the account class call the depositmoney() of the account class and not the one from the Sav_account class? The direct call to depositmoney() method calls method of the sub-class which is obvious. But can't understand why the indirect call from operations() does not give the expected result.
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int temp = 0;
class account
{
protected:
string name;
double balance;
int AccNo;
public:
void operations()
{
int r = 0;
do{
cout << "1.DEPOSIT" << endl;
cout << "2.WITHDRAW" << endl;
cout << "3.CHECK BALANCE" << endl;
cout << "0.EXIT" << endl;
cin >> r;
switch(r)
{
case 1:this->depositmoney();
break;
case 2:this->withdraw();
break;
case 3:this->displaybalance();
break;
case 0:cout << "Exiting" << endl;
break;
default:
break;
}
}
while(r != 0);
}
account(string Name, double bal)
{
name = Name;
balance = bal;
AccNo = temp++;
}
void displaybalance()
{
cout << "name :" << name << endl;
cout << "A/C" << AccNo << endl;
cout << "your balance is " << balance << endl;
}
void depositmoney()
{
float deposit;
cout << "enter deposit" << endl;
cin >> deposit;
balance += deposit;
}
protected:
void withdraw()
{
float withdrawal;
cout << "enter witdrawal amount" << endl;
cin >> withdrawal;
if(balance >= withdrawal)
{
balance -= withdrawal;
}
else
{
cout << "insufficient funds" << endl;
}
}
};
class Sav_account :public account
{
private:
double const CI = 5;
void depositinterest()
{
balance += balance*CI / 100;
}
public:
Sav_account(string Name, double bal) :account(Name, bal)
{
AccType = 0;
}
void depositmoney()
{
account::depositmoney();
depositinterest();
}
};
void main()
{
Sav_account *account1 = new Sav_account("Shaw", 50000);
account1->operations();// DEPOSIT RESULTS IN NO INTEREST
account1->displaybalance();
account1->depositmoney(); // **DEPOSIT FUNCTION OF Sav_account CALLS interest function**
account1->displaybalance();
_getch();
}
Your class is not polymorphic. You have no polymorphic functions and have not overridden anything. (You have hidden functions instead). As such, calling depositmoney(); from a function of account will call account::depositmoney().
To make a class polymorphic you need to use virtual functions. Any function that you want to have polymorphic behaviour needs to be declared as virtual in the base class, e.g. in account here:
virtual void depositmoney() {
In the derived class, since C++11, you can write:
void depositmoney() override
^^^^^^^^
which will cause a compiler error if you accidentally hide a function when you are trying to override it. (Although I'm guessing that you are using a compiler that doesn't support C++11, since most of those will also reject void main).
Also, any polymorphic base class should have a virtual destructor, i.e. virtual ~account() {} otherwise the code account *x = new Sav_account("Shaw", 50000); delete x; would cause undefined behaviour.
void main is illegal, main must have a return type of int.
There aren't any overridden methods in your classes; you haven't made any of them virtual.