Hello so i want to create a header file class which name testing and also its cpp but for some reason this is inaccessible i dont know why
testing.h
#ifndef TESTING_H
#define TESTING_H
#include <string>
using namespace std;
class testing
{
string Name;
void printname(string name);
};
#endif
testing.cpp
#include <iostream>
#include "testing.h"
using namespace std;
void testing::printname(string name) // inaccessible in my main i dont know what reason :(
{
Name = name;
cout<<Name<<endl;
}
main
#include <iostream>
#include "testing.h"
using namespace std;
using std::string;
int main()
{
testing tester;
tester.printname("JPR"); //error since testing::printname is inaccessible no idea
return 0;
}
If you don't specify the visibility of the members, they are private.
You can either use a struct (visibility is public):
struct testing
{
string Name;
void printname(string name);
};
or you can specify that printname is public:
class testing
{
public:
void printname(string name);
private:
string Name;
};
Try the following:
testing.h
#ifndef TESTING_H
#define TESTING_H
#include <string>
class testing
{
public:
// Better to pass the parameter as const reference to avoid performing a copy.
void printname(const std::string& name);
private:
std::string Name;
};
#endif
testing.cpp
#include <iostream>
#include "testing.h"
void testing::printname(const std::string& name)
{
Name = name;
std::cout << Name << std::endl;
}
main.cpp
#include "testing.h"
int main()
{
testing tester;
tester.printname("JPR");
return 0;
}
In C++:
A class defined with the keyword class has private access for its members and its base classes by default.
Add public: access modifier to your class defition, to mark method as public:
class testing
{
string Name;
public:
void printname(string name);
};
when you create a class, every member function and member variable is set in default as private, which means that they won't be accessible. To make your function public you need to change this in your code:
class testing
{
private: //is private in default, i add it for better readabilty
string Name;
public:
void printname(string name);
};
Worth to mention that you (almost) ALWAYS want to keep all member variables private!
Related
(Beginner in OOP.)
I have a person class which stores the name of the person. In a derived class instance printer, I need that name so that I can do further tasks with it.
#include <iostream>
#include <string>
class Person{
public:
std::string name;
};
class Printer: public Person{
public:
void print(){
printf("%s", this->name.c_str());
}
};
int main() {
Person one;
one.name = "john";
Printer printer;
printer.print();
return 0;
}
What am I not doing to have printer see the data in one ? I'd be having several printer-like objects so storing "john" only once is the goal here.
You have made the member vars public but this is not the right approach for the OOP, the right one is to have them under private access specifier and use setters and getters.
Now to your mistakes:
You use void for main but with c++ you can only use int for it.
You use std::string as an argument for printf but it can't accept it. (I passed the c_string of the std::string to correct that).
You use an object, from the parent class, and give it a name then use another object, from the driven one, to print the name of the first one. (I used only one)
#include <iostream>
#include <string>
class Person{
public:
std::string name;
};
class Printer: public Person{
public:
void print(){
printf("%s",this-> name.c_str());
}
};
int main() {
Printer printer;
printer.name = "name";
printer.print();
}
After your comments, I have updated Printer class to do your inttent
#include <iostream>
#include <string>
class Person{
public:
std::string name;
};
class Printer{
public:
void print(const Person& person ){
printf("%s", person.name.c_str());
}
};
int main() {
Person one;
one.name = "name";
Printer printer;
printer.print(one);
}
I'm coding simple shop program in cpp. I have 3 classes: Shop, Client, Bucket. Shop is parent class for Bucket. Shop has vector of clients and every client has his own bucket.
I've got problem with #include's. I have to include Client.h in Shop.h so the Shop could see the vector of clients, but it seems I also have to include Bucket.h in Clinet.h for similar reason.
This generates a problem: Bucket is being included before Shop so I get 'Base Class Undefined' error.
How can I make this work?
Shop.h
#pragma once
#include <vector>
#include <string>
#include "functions.h"
#include "Client.h"
class Shop {
protected:
std::vector<int> quantities;
std::vector<std::string> products;
std::vector<float> prices;
private:
std::vector<Client*> clients;
int loggedClient;
public:
Shop();
~Shop();
int readProducts();
int loadClientsBase();
int checkLoginData(std::string log, std::string pass, int *logged);
int checkIfSameLogin(std::string log);
int addClient();
void login();
void logout();
int sell();
virtual void display();
void displayLoggedClient();
int saveHistory();
};
Client.h
#pragma once
#include "Bucket.h"
class Client
{
private:
float money=0.0;
Bucket bucket;
std::string login;
std::string password;
std::string description;
public:
Client();
~Client();
void addLoginData(std::string log, std::string pass, std::string desc, float mon);
std::string getLogin() { return login; };
std::string getPassword() { return password; };
std::string getDescription() { return description; };
float getMoney() { return money; };
void addLogin(std::string log);
void addPassword(std::string pass);
void addDescription(std::string desc);
void addMoney(float m);
void addToBucket(std::string prod, int quant, float price);
void displayBucket();
Bucket getBucket() { return bucket; };
friend std::ostream& operator<<(std::ostream& os, Client& client);
};
Bucket.h
#pragma once
#include <vector>
#include <string>
#include "Shop.h"
class Bucket : public Shop
{
public:
Bucket();
~Bucket();
void addProduct(std::string name, int amount, float price);
void deleteProduct();
void display();
std::string getProduct(int i);
int getQuantity(int i);
float getPrice(int i);
int getNumberOfProducts() { return products.size(); };
void clearBucket();
};
Bucket.h includes Shop.h which includes Client.h which includes Bucket.h... And so on forever and ever. This is a circular dependency.
Shop.h doesn't need to include Client.h, it only needs to forward declare the Client class:
#pragma once
#include <vector>
#include <string>
#include "functions.h"
// #include "Client.h"
class Client; // Forward declaration of the class, that's needed for pointers to it
class Shop {
...
};
The implementation of Shop needs the full definition of Client, so the Shop source file (Shop.cpp?) needs to include Client.h.
To solve the immediate issue, you'll need to forward declare Client class in Shop.h. However the class hierarchy you are building here is confusing to say the least, e.g. each Client owns a Shop, changing the class hierarchy to something more sensible should remove the problem altogether.
#include <iostream>
#include<string>
using namespace std;
class Human
{
private:
string Name;
int Age;
friend class Utility;
public:
Human(string InputName,int InputAge)
{
Name = InputName;
Age = InputAge;
}
};
class Utility
{
public:
void DisplayAge(const Human& Person)
{
cout<<Person.Age<<endl;
}
};
int main()
{
Human FirstMan("Adam",25);
cout<<"Accessing private member Age via friend class: ";
Utility::DisplayAge(FirstMan);
}
I don't understand..when I call the function I do send an object(FistMan)..why my compiler still says that I call it without object?
DisplayAge in Utility is not a static function. Therefore you need an instance of Uitility in order to call it.
So, either make the function static, or call it via an anonymous temporary
Utility().DisplayAge(FirstMan);
Better still, make DisplayAge a member function of Human.
Use the static keyword and then you'll be able to call your function on your class
I edited your code below :
#include <iostream>
#include<string>
using namespace std;
class Human
{
private:
string Name;
int Age;
friend class Utility;
public:
Human(string InputName,int InputAge)
{
Name = InputName;
Age = InputAge;
}
};
class Utility
{
friend class Human;
public:
Utility() = default;
static void DisplayAge(const Human& Person)
{
cout<<Person.Age<<endl;
}
};
int main(void)
{
Human FirstMan("Adam",25);
cout<<"Accessing private member Age via friend class: ";
Utility::DisplayAge(FirstMan);
}
Dön't use a class whenever you want to define functions. Use a namespace:
namespace Utility
{
inline void DisplayAge(const Human& Person)
{
cout<<Person.Age<<endl;
}
}
int main()
{
Human FirstMan("Adam",25);
cout<<"Accessing private member Age via friend class: ";
Utility::DisplayAge(FirstMan);
}
I'm getting this->
error -type ‘Ship’ is not a direct base of ‘CruiseShip’-
I cannot figure it out. This is where the error is occurring I am assuming. I am not really sure I guess how I should be calling the base class?
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
//A print function that overrides the print function in the base class.
//The CruiseShip class's print function should display only the ship's
//name and the maximum number of passengers.
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include <string>
using namespace std;
class Ship;
class CruiseShip{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.cpp
#include "Ship.h"
#include <iostream>
using namespace std;
string name;
string built;
Ship::Ship(){
}
Ship::Ship(string n, string b)
{
name = n;
built = b;
}
//accessors and mutators methods
string getName()
{
return name;
}
string getBuilt()
{
return built;
}
//A virtual print function that displays
//the ship's name and the year it was built
void print()
{
cout<<"Name:"<<getName()<<"\nYear built:"<<getBuilt()<<endl;
cout<<"-------------------------"<<endl;
}
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You need to derive CruiseShip from Ship:
class CruiseShip : public Ship {
this:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
^^^^^^^^^^^^
is a base class constructor call, but you have not derived CruiseShip from Ship, yet compiler knows that Ship is a class type.
Im trying to use Inheritance through multiple header and cpp files for a text game that I'm writing.
I have my base class of Weapon. Which is in the file Weapon.h
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
I am then trying to inherit form the base Weapon.h class to a Bow and Sword class. I am sure I am including the file correctly but when I try to compile I get the error "error: expected class name class Blade : public Weapon" The same error for the Bow class.
#include "Weapon.h"
#include "Crossbow.h"
using namespace std;
class Bow : public Weapon
{
public:
string Type = "Ranged";
bool loaded;
protected:
Bow();
};
#include "Weapon.h"
class Blade : public Weapon
{
private:
string Type = "Melee";
protected:
void Draw();
};
Does anyone know why this is happening? Google isn't coming up for anything useful either. Thanks
MCVE (I think)
//In Base.h
class Base
{
public:
int function();
private:
};
//In Base.cpp
int Base::function()
{
randomshit
return 0;
}
//In Inherit.h
#include "Base.h"
class Inherit : public Base
{
public:
int function():
private:
};
Getting error: "expected class name class Bow : public Weapon"
EDIT: Turns out I needed to include "#pragma once" and that solved almost everything. Thanks for the help guys.
You don't use any include guards, so your file Weapon.h is probably included multiple times, leading to the compilation error.
To know more about include guards: https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Include_Guard_Macro
Your header Weapon.h would then become:
#ifndef WEAPON_H_INCLUDED
#define WEAPON_H_INCLUDED
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
#endif // WEAPON_H_INCLUDED
Do the same for all other header files.
Once you did that, remove all unnecessary includes and do a clean rebuild.
This is maybe not an answer but it just is impossible to post it as comment
This compiles (but does not link !!) on my Visual Studio 2013.
#include <string>
using namespace std;
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
class Bow : public Weapon
{
public:
string Type = "Ranged";
bool loaded;
protected:
Bow();
};
class Blade : public Weapon
{
private:
string Type = "Melee";
protected:
void Draw();
};
But it is likely to fail on older compilers because of the initialisation in the declaration as string Type = "Melee";.
Note that using namespace std; comes before the declaration of class Weapon.