C++ output operator for abstract class - c++

So I have an abstract class Product with fields name and price. There are a few classes that inherit from Product, and the reason Product is abstract is because these subclasses have to implement this function (defined in Product):
virtual std::string getCategory()=0;
Category is not a field, it just depends on which subclass we have and in some cases on the price.
Now I want an output operator for the subclasses of Product, but since I only want to print the name and price, I did this in Product.h:
friend std::ostream& operator<<(std::ostream& os, const Product& secondOperand);
And this in Product.cpp:
ostream& operator<<(ostream& outputStream, Product& secondOperand){
outputStream << "["<<secondOperand.getName()<<" "<<secondOperand.getPrice()<<"]"<<endl;
return outputStream;
}
Now I get this error in visual studio:
Error C2259: 'Product' : cannot instantiate abstract class
I don't want to implement this output for every subclass (cause then I have to literally copy everything which isn't ideal). Also, I started out with Product being not pure virtual, but then I had Linker errors for the getCategory() functions...

Nothing wrong with the approach, here's a sample which compiles and runs....
#include <string>
#include <iostream>
using namespace std;
class Product
{
public:
friend std::ostream& operator<<(std::ostream& os, const Product& secondOperand);
virtual ~Product() = 0;
string getName() { return "Product Name"; }
string getPrice() {return "£1.00"; }
virtual std::string getCategory()=0;
};
ostream& operator<<(ostream& outputStream, Product& secondOperand){
outputStream << "["<<secondOperand.getName()<<" "<<secondOperand.getPrice()<<"]"<<endl;
return outputStream;
}
Product::~Product() {}
class DerivedProduct : public Product
{
public:
DerivedProduct() {}
std::string getCategory() { return "Derived getCategory()"; }
};
int main(int argc, char *argv[])
{
DerivedProduct d;
cout << d.getCategory() << endl;
cout << d << endl;
return 0;
}

Related

How to invoke an overloaded parent cout friend class from a derived class?

Imagine a setup as follows. How do I invoke the base class cout from within the derived class cout? I could use the getBrand() method, but I feel like I should be able to directly access the base class' cout friend function.
I hacked a bit, and tried this.Brand and also just Brand. No luck.
class Brand {
public:
Brand(std::string brand):brand_(brand) {};
friend std::ostream & operator << (std::ostream & out, const Brand & b) {
out << b.brand_ << ' ';
return out;
}
std::string getBrand()const { return brand_; }
private:
std::string brand_;
}
class Cheese : public Brand {
public:
Cheese(std::string brand, std::string type):Brand(brand), type_(type) {};
friend std::ostream & operator << (std::ostream & out, const Cheese & c) {
out << /* THIS.BRAND?! BRAND?! getBrand() meh.. */ << ' ' << c.type_ << std::endl; // <-- HERE
return out;
}
private:
std::string type_;
}
int main() {
Cheese c("Cabot Clothbound", "Cheddar");
std::cout << c << std::endl;
}
DESIRED OUTPUT
Cabot Clothbound Cheddar
You could invoke the overloaded operator << of the Base class from the derived class. Since, you declared the operator as a friend, you could simply cast the the derived class to a base class:
class Cheese : public Brand {
public:
Cheese(std::string brand, std::string type):Brand(brand), type_(type) {};
friend std::ostream & operator << (std::ostream & out, const Cheese & c) {
//ADDED
out << static_cast<const Brand&>(c) << c.type_ << std::endl;
return out;
}
private:
std::string type_;
};
Output:
Cabot Clothbound Cheddar
See it Live
Cast it, like this:
friend std::ostream& operator<<(std::ostream& out, const Cheese& c)
{
out << static_cast<const Brand &>(c);
out << c.type_ << std::endl;
return out;
}
All other answers are responding correctly to your specific question, but whenever you'll try to use polymorphism like this:
Brand const &c = Cheese("Cabot Clothbound", "Cheddar");
std::cout << c << std::endl;
operator << corresponding to Brand will be called instead of Cheese's.
The good way to do it is to use a virtual print member function:
class Brand {
public:
Brand(std::string const & brand):brand_(brand) {}
virtual ~Brand() {}
virtual void print(std::ostream & out) const {
out << brand_;
}
std::string const & getBrand()const { return brand_; }
private:
std::string brand_;
};
class Cheese : public Brand {
public:
Cheese(std::string const & brand, std::string const & type):Brand(brand), type_(type) {}
void print(std::ostream & out) const override {
Brand::print(out); // calling base print()
out << ' ' << type_ << std::endl;
}
private:
std::string type_;
};
Then, you only need a single operator << for the base class which will call your print virtual member function:
std::ostream & operator << (std::ostream & out, const Brand & b) {
b.print(out);
return out;
}
DEMO
You obviously can't do anything like Brand::operator<<, because both operator<< are defined as friend and thus they are not member functions.
If you want to invoke operator<<(std::ostream&, const Brand&), you just have to pass correct types to that, and since Derived classes can be easily casted to Base classes, you can just do
friend std::ostream & operator << (std::ostream & out, const Cheese & c) {
out << static_cast<const Brand&>(c) << ' ' << c.type_ << std::endl;
return out;
}

C++ how to invoke a child's operator<<(ostream) from an array/collection of base elements

Say I have a Person class with a field string name. Now let's have a class Student deriving Person with a field int avgGrade. Both classes have the operator<< defined.
I want to be able to store in an array or similar structure elements of type Person, but also be able to store derived class objects in it. I'll be later traversing the elements and want to use the operator<< and actually call that specific object's definition for that operator, instead of always the base version of it.
How would I go about doing that? In what structure and what kind type of elements should I store?
Here's the current collection of classes:
Person.h:
#pragma once
#include <iostream>
class Person
{
private:
std::string name;
public:
Person();
Person(std::string);
friend std::ostream& operator<<(std::ostream& os, const Person& obj);
}
Person.cpp:
#include "Person.h"
Person::Person() : Person("default") { }
Person::Person(std::string name)
{
this->name = name;
}
std::ostream& operator<<(std::ostream& os, const Person& obj)
{
os << "Name: " << obj.name;
return os;
}
Student.h:
#pragma once
#include "Person.h"
#include <iostream>
class Student : Person
{
private:
double avgGrade;
public:
Student();
Student(const std::string cs, const double avg_grade);
friend std::ostream& operator<<(std::ostream& os, const Student& obj);
};
Student.cpp:
#include "Student.h"
Student::Student() : Student("default", 4) { }
Student::Student(const std::string cs, const double avg_grade)
: Person(cs),
avgGrade(avg_grade)
{
this->avgGrade = avg_grade;
}
std::ostream& operator<<(std::ostream& os, const Student& obj)
{
os << (Person)obj << std::endl;
os << "Average grade: " << obj.avgGrade;
return os;
}
Demo.cpp:
#include "Person.h"
#include "Student.h"
#include <iostream>
int main(int argc, char* argv[])
{
Person p("john");
Student s("johana", 5);
Person* arr[2];
arr[0] = &p;
arr[1] = &s; // Error: conversion to inaccessible base class "Person" is not allowed
std::cout << arr[0] << std::endl;
std::cout << arr[1] << std::endl;
return 0;
}
The general solution to this sort of problem is to declare:
inline std::ostream& operator<<(std::ostream& str, const Base& o)
{
o.print(str);
return str;
}
as a non-member function, then:
virtual void print(std::ostream& str);
in Base, and override in Derived as required. (The Derived version may well start with Base::print(str); to call the base version.)
Your declaration of array is fine, but you would then print the elements with:
std::cout << *arr[0] << std::endl;
std::cout << *arr[1] << std::endl;
The problem with initializing the array, is that by default, base classes are private. Fix that with:
class Student : public Person ...

Overloading the Output Stream Operator for a Class

Suppose this is my class:
class Student {
std::string name;
int CWID;
public:
Student(std::string name = "N/A", int CWID = 99999999) : this->name(name), this->CWID(CWID) {}
};
Now, how do I overload the output stream operator << that will print all the data in the class. I'm guessing this is equivalent to the toString() method in Java but kindly show me how to do it in C++.
Add member functions to the class that return the name and CWID.
std::string getName() const {return name;}
int getCWID() const {return CWID;}
Then, add a non-member function to write the data out to a stream.
std::ostream& operator<<(std::ostream& out, Student const& s)
{
return out << s.getName() << " " << s.getCWID();
}
Here is how you do it:
class Student {
std::string name;
int CWID;
public:
Student(std::string name = "N/A", int CWID = 99999999) : name(name), CWID(CWID) {}
friend std::ostream& operator<<(std::ostream& stream, const Student& student);
};
std::ostream& operator<<(std::ostream& stream, const Student& student)
{
stream << '(' << student.name << ", " << student.CWID << ')';
return stream;
}
Please note that this overloaded function is not part of the class.
You could write non-member function
std::ostream& operator<<(std::ostream& os, const Student& stud)
{
os << stud.name << " " << stud.CWID;
return os;
}
and declare it as friend function in your class
class Student {
std::string name;
int CWID;
public:
Student(std::string name = "N/A", int CWID = 99999999) : this->name(name), this->CWID(CWID) {}
friend ostream& operator<<(ostream& os, const Student& stud);
};

Overriding of operator<< in c++

I'm working on a project for my school in C++
I have 2 class : Employe and Teacher.
Teacher derived from Employe and has overrides of his functions.
We override the operator << to print some information of the Employes or Teachers.
Each class has a const int attribute LevelAcces_.
For Employe, it's 5 and for Teacher it's 20.
when I create an Teacher in my main.cpp, I call the override of operator<< of Teacher to print his information.
So this function is called :
ostream& operator<<(ostream& os, const Teacher& pTeacher){
os << (pTeacher);
return os;
}
But, the function calls itself with the line "os << (pTeacher);" and does a loop that causes a stack overflow.
I want that the line "os << (pTeacher)" calls the operator<< of my class Employe and not of my class Teacher.
Override of operator<< in Employe :
ostream& operator<<(ostream& os, const Employe& pEmploye){
os << "Name: " << pEmploye.name_ << endl;
os << "Class: " << pEmploye.getClass() << endl;
os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;
return os;
}
I tried to cast my Teacher into Employe but when it prints the message, LevelAcces is 5 (and I want 20, because my Employe is a Teacher).
I also tried to use Employe::operator<< but operator<< is not a member of Employe so it doesn't work...
So, here is my question :
How can I do to use my operator<< of Employe in my operator<< of Teacher and print the right information (LevelAccess = 20 and not 5) ?
I was also thinking of "virtual" but our professor tells us that there is not need to use this word.
Thanks in advance :)
Here is a more complete code :
main.cpp:
Teacher Garry("Garry");
cout << Garry << endl;
Employe.cpp :
#include "Employe.h"
using namespace std;
Employe::Employe(){
name_ = "";
}
Employe::Employe(string pName){
name_ = pName;
}
string Employe::getName() const{
return name_;
}
unsigned int Employe::getLevelAccess() const{
return levelAccess_;
}
string Employe::getClass() const{
return typeid(*this).name();
}
ostream& operator<<(ostream& os, const Employe& pEmploye){
os << "Name: " << pEmploye.name_ << endl;
os << "Class: " << pEmploye.getClass() << endl;
os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;
return os;
}
With this in Employe.h :
private:
static const unsigned int LevelAccess_ = 5;
Teacher.cpp :
#include "teacher.h"
using namespace std;
Teacher::Teacher(string pName){
nom_ = pName;
}
unsigned int Teacher::getLevelAccess() const{
return(Employe::getLevelAccess() + accessTeacher_);
}
string Teacher::getClass() const{
return typeid(*this).name();
}
ostream& operator<<(ostream& os, const Teacher& pTeacher){
os << (pTeacher);
return os;
}
With this is Teacher.h :
static const unsigned int accesTeacher_ = 15;
What I'd do is the following: define only one
ostream& operator<<(ostream& os, const Employe& pEmploye)
{
return pEmploye.display(os);
}
for the base of the hierarchy,
in which you call a protected member function virtual display() that is overridden by each derived class and to which the display is being delegated. This is sometime call the NVI (non-virtual interface) idiom. It works like this:
class Employee
{
// ...
friend ostream& operator<<(ostream& os, const Employee& pEmployee)
{
return pEmployee.display(os);
}
protected:
virtual ostream& display(ostream& os) const
{
// implement it here
return os;
}
};
class Teacher: public Employee
{
// ....
protected:
ostream& display(ostream& os) const override
{
// implement it here
return os;
}
};
You can use a cast:
os << static_cast<const Employe &>(pTeacher);
The & is important.
To make the call to the member function call Teacher::getLevelAccess() from an Employe reference, you have to make that function virtual. (Do this in teacher.h). getClass() should be virtual also.
NB. You keep saying things like "Override of operator<< in Employe :" , however you do not have overloaded operator<< in Employe . You have a free function which takes Employe as argument.

C2248 error with friend function

I'm writing a basic overload of the << operator so I've added a friend function inside the class interface
namespace Warehouse
{
namespace Dto
{
class Product;
class AbstractOrder : public ICloneableItem
{
protected:
unsigned long _id;
std::string _name;
std::vector<Product*> _products;
public:
AbstractOrder();
virtual ~AbstractOrder();
double computePrice() const;
void addProduct(Product* product);
void removeProduct(Product* product);
void removeAllProducts();
void setName(const std::string& name) { _name = name; }
std::string getName() const { return _name; }
unsigned long getId() const { return _id; }
std::vector<Product*> getProductList() const { return _products; }
friend std::ostream& operator<<(std::ostream& os, const AbstractOrder& ord);
};
}
}
Inside the implementation file this is the code of the function
using namespace Warehouse::Dto;
....
std::ostream& operator<<(std::ostream& os, const AbstractOrder& ord)
{
os << "[" << ord._id << "] Order " << ord._name << ": " << ord.computePrice();
return os;
}
Why I'm getting the following error?
Error 1 error C2248: 'Warehouse::Dto::AbstractOrder::_id' : cannot access protected member declared in class 'Warehouse::Dto::AbstractOrder'
Actually I've already fixed it,prepending the namespace before the operator<< on the implementation file.
What I don't understand is why I have to do it even if in the implementation file I've used a using namespace Warehouse::Dto directive?
Because the operator << is defined in the global namespace and the AbstractOrder class is defined in the Warehouse::Dto namespace. But the friend declaration is for a stream operator in the Warehouse::Dto namespace.
If you want to defined proper friend declaration for the operator in the global namespace is would be:
friend std::ostream& ::operator<<(std::ostream& os, const AbstractOrder& ord);
But then again you want the operator to live in the same namespace as the class it is streaming.