My current code is not working.
I am trying to use << operator of Person class in the << operator of Student class. Is that possible?
#include <iostream>
using namespace std;
class Person
{
private:
int EGN;
public:
Person(int e):EGN(e){}
friend ostream& operator <<(ostream& out, const Person& p);
};
ostream& operator <<(ostream& out, const Person& p)
{
out<<p.EGN<<endl;
return out;
}
class Student: public Person
{
private:
int fn;
public:
Student(int e, int f):Person(e)
{
fn=f;
}
friend ostream& operator <<(ostream& out, const Student& p);
};
ostream& operator <<(ostream& out, const Student& p)
{
Person :: operator << (out,p);
out<<p.fn<<endl;
return out;
}
Person's operator << is a friend, not a member, so you can't access it using the :: operator.
Try casting your student to a person to call the right overload:
out << static_cast<const Person &>(p);
Related
I was just learning about friend classes and overloading operators in C++ where I came upon an issue where I cannot access the private variables in the friend class. I'm almost sure this has to do with the fact we were required to have each file separate (Header, .cpp and main). Need some fresh eyes.
// Contents of Percent.h
#ifndef PERCENT_H
#define PERCENT_H
#pragma once
class Percent
{
// friend const Percent operator >>(Percent& first, Percent& second);
public:
friend bool operator ==(const Percent& first,
const Percent& second);
friend bool operator <(const Percent& first,
const Percent& second);
Percent();
friend istream& operator >>(istream& inputStream,
Percent& aPercent);
friend ostream& operator <<(ostream& outputStream,
const Percent& aPercent);
//There will be other members and friends.
private:
int value;
};
// Contents of Percent.cpp
#include <iostream>
#include "Percent.h"
using namespace std;
istream& operator >>(istream& inputStream,
Percent& aPercent)
{
char percentSign;
ERROR HERE - Cannot access value
inputStream >> aPercent.value;
inputStream >> percentSign;
return inputStream;
}
ostream& operator <<(ostream& outputStream,
const Percent& aPercent)
{
outputStream << aPercent.value << '%';
return outputStream;
}
#endif // !PERCENT_H
// Main.cpp
not written yet
I have 3 classes which creates a complete Binary search tree. The 3 classes are
1. DBentry(stores a name, IP address, and status),
2. TreeNode(points to its own DBentry, as well as entries to its left and right)
3. TreeDB(contains a root TreeNode and provides various functions to add, remove, update, and find DBentryobjects)
Inside DBentry I have friend ostream& operator <<(ostream& out, const DBentry& rhs);
Inside TreeDB I have friend ostream& operator<< (ostream& out, const TreeDB& rhs);
friend ostream& operator <<(ostream& out, TreeNode* rhs);
These overloading operators doesn't seem to work properly. Any help would be really helpful.
Class DBentry:
class DBentry {
private:
string name;
unsigned int IPaddress;
bool active;
public:
DBentry();
DBentry (string _name, unsigned int _IPaddress, bool _active);
~DBentry();
void setName(string _name);
void setIPaddress(unsigned int _IPaddress);
void setActive (bool _active);
string getName() const;
unsigned int getIPaddress() const;
bool getActive() const;
friend ostream& operator <<(ostream& out, const DBentry& rhs);
};
Class TreeNode:
class TreeNode {
private:
DBentry* entryPtr;
TreeNode* left;
TreeNode* right;
public:
TreeNode();
TreeNode(DBentry* _entryPtr);
~TreeNode();
void setLeft(TreeNode* newLeft);
void setRight(TreeNode* newRight);
TreeNode* getLeft();
TreeNode* getRight();
DBentry* getEntry() const;
bool find(string _name);
};
Class TreeDB has private:
TreeNode* root;
ostream& operator <<(ostream& out, const DBentry& rhs){
out<<rhs.name<<" : "<<rhs.IPaddress<<" : ";//<<rhs.active? (out<<"active"):(out<<"inactive")<<endl;
if(rhs.active)
out<<"active";
else
out<<"inactive";
out<<endl;
}
ostream& operator <<(ostream& out, TreeNode& rhs){
if(rhs.getEntry()!=NULL){
out << *(rhs.getLeft());
out << *(rhs.getEntry());
out << *(rhs.getRight());
}
}
ostream& operator<< (ostream& out, const TreeDB& rhs){
out << *(rhs.root);
}
ostream& operator <<(ostream& out, TreeNode& rhs) says the function returns a reference to an ostream. The code does not return an ostream reference, so the program will go on a merry little adventure into Undefined Behaviour.
At the very least, and there may be other problems in the unposted portions of the program, OP must
ostream& operator <<(ostream& out, TreeNode& rhs){
if(rhs.getEntry()!=NULL){
out << *(rhs.getLeft());
out << *(rhs.getEntry());
out << *(rhs.getRight());
}
return out; //<-- return the stream. Do not cross streams unless fighting Gozer.
}
The other operator<< overloads have the same flaw.
I am a beginner trying to learn c++ so probably my question is very basic. Consider the following pice of code:
class pounds
{
private:
int m_p;
int m_cents;
public:
pounds(){m_p = 0; m_cents= 0;}
pounds(int p, int cents)
{
m_p = p;
m_cents = cents;
}
friend ostream& operator << (ostream&, pounds&);
friend istream& operator>>(istream&, pounds&);
};
ostream& operator<< (ostream& op, pounds& p)
{
op<<p.m_p<<"and "<<p.m_cents<<endl;
return op;
}
istream& operator>>(istream& ip, pounds& p)
{
ip>>p.m_p>>p.m_cents;
return ip;
}
This compiles and seems to work but I am not returning a reference to a local variable? Thanks in advance.
It's correct, since there are no local variables, there are references, that will be passed, when operators will be called.
And i suggest you to change signature of operator << to
std::ostream& operator << (ostream& os, const pounds& p);
since, p is not modified in function.
Problem Solved, thank you all for your help!
I've run into a problem that i can't seem to figure out.
I am trying to overload the ostream operator as a friend function in order to be able to print out the member data of that object, and i can't seem to get it to work.
This is what i got so far:
.h file:
Class TestIt:
{
public:
TestIt();
TestIt(int a, b);
friend ostream& operator <<(ostream& outputStream, const TestIt& a);
Private:
int NUMBER1;
int NUMBER2;
};
.cpp file:
ostream& operator <<(ostream& outputStream, const TestIt& a)
{
outputStream << a.NUMBER1 << " " << a.NUMBER2;
return(outputStream);
}
What i am trying to do is, pass in an object in ostream, then output its member data.
The error that i am receiving is that
"the member TestIt::NUMBER1 declared in TestIt.h is inaccessible.
and same error also exists with my other member data.
Why would that be?
Thank you for your help.
Here's a whole program that i just wrote, giving me the same error:
TestClass.cpp
#include "TestClass.h"
#include <iostream>
using namespace std;
TestClass::TestClass(int a, int b)
{
age = a;
whole = b;
}
int TestClass::GetAge() const
{
return(age);
}
ostream& operator <<(ostream& outputStream, const TestClass& t1)
{
t1.whole;
t1.age;
return(outputStream);
}
TestClass.h
#ifndef TestClass_H
#define TestClass_H
class TestClass
{
public:
TestClass(int a, int b);
int GetAge() const;
friend ostream& operator <<(ostream& outputStream, const TestClass& t1);
private:
int whole;
int age;
#endif
The operator<< that you've defined and the operator<< that you've friended aren't the same name. Are you using namespaces?
Given a class such as:
class Person
{
private:
char *name;
public:
Person()
{
name = new char[20];
}
~Person()
{
delete [] name;
}
}
I want to print to print the name from an instance of this, using a statement like the following:
cout << myPerson << endl;
What do I need to do to define the << output operator for this class?
add this in the class:
friend std::ostream& operator<< (std::ostream& out, const Person& P);
and then define the operator<< something like this:
std::ostream& operator<< (std::ostream& out, const Person& P) {
out << P.name;
return out;
}
Define a member function print() that takes an ostream as an argument. Then let the overloaded operator<< call this member function. This way you can avoid using friend. Example:
void YourClass::print(ostream& out) const
{
//implement printing ...
}
ostream& operator<<(ostream& out, const YourClass& m)
{
m.print(out);
return out;
}