take a look at friend function i am trying to implement
#include <iostream>
#include <string>
using namespace std;
class Customer {
friend void displayInfo(Customer, City);
private:
int custNum;
int zipCode;
};
class City {
friend void displayInfo(Customer, City);
string cityName;
int zipCode;
};
void displayInfo(Customer cus, City city) {
cout << cus.custNum; //compiler error - Inaccessible
}
I understand it is inaccessible. However i have the friend function already defined in the class. So why it is not accessible? Thank you
While you are declaring displayInfo() function as a friend of City and Customer classes, the classes City and Customer given to displayInfo as parameters(i.e. in the declaration) are not defined yet.
If you simply add two lines to the top of your code, as shown here, it does compile.
class City;
class Customer;
Tested code. Only necessary to add class City;
#include <iostream>
#include <string>
using namespace std;
class City;
class Customer {
friend void displayInfo(Customer, City);
private:
int custNum;
int zipCode;
};
class City {
friend void displayInfo(Customer, City);
string cityName;
int zipCode;
};
void displayInfo(Customer cus, City city) {
cout << cus.custNum; //compiler error - Inaccessible
}
Related
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!
#include <iostream>
using namespace std;
class Date{
private:
int day;
int month;
int year;
public:
Date(int dy,int mt,int yr){
day=dy;
month=mt;
year=yr;
}
void showDate(){
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
};
class Human{
private:
string name;
Date birthDay;
public:
Human(string nm,Date bd):name(nm),birthDay(bd){};
showHumanInfo(){
cout<<"The person named : "<<name<<" was born : ";
birthDay.showDate();
}
};
int main()
{
Date birthday(1,2,1995);
Human h1("alek",birthday);
h1.showHumanInfo();
return 0;
}
This works, but why it doesn't work when I do the following?
#include <iostream>
using namespace std;
class Date{
private:
int day;
int month;
int year;
public:
Date(int dy,int mt,int yr){
day=dy;
month=mt;
year=yr;
}
void showDate(){
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
};
class Human{
private:
string name;
Date birthDay;
public:
Human(string nm,Date bd){
name = nm;
birthDay = bd;
}
showHumanInfo(){
cout<<"The person named : "<<name<<" was born : ";
birthDay.showDate();
}
};
int main()
{
Date birthday(1,2,1995);
Human h1("alek",birthday);
h1.showHumanInfo();
return 0;
}
I have problem like that. Why can't I use date class in a human class?
When I change human public class like that
public:
human(){
// ...
}
It not working it thing is the same think but not adding date class in human class.
In the definition of a constructor, all member variables must be initialized before the body of the constructor is executed. Since Date doesn't have a default constructor, there is no way to initialize it
Human(string nm, Date bd)
{ // birthDay must be initialized before this point
// ...
birthDay = bd; // this is assignment, which is too late
}
The fix is to either give Date a default constructor, if that makes sense, or to initialize birthDay in a member initializer list, as you did in the first example code.
I think problems comes from "birthday = bd;"
It has been a while since the last time I coded in C++ but if I am not wrong, using constructor initialiser and using equal operator are not the same.
You should override "=" operator to use it with your object.
About his
(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);
}
#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.