Skip to content

Instantly share code, notes, and snippets.

@debdutdeb
Last active April 12, 2021 07:22
Show Gist options
  • Save debdutdeb/9a7d6684e5ea503630276284e9a3ce75 to your computer and use it in GitHub Desktop.
Save debdutdeb/9a7d6684e5ea503630276284e9a3ce75 to your computer and use it in GitHub Desktop.
I like how C++ programs look
#include <iostream>
class Printable {
public:
virtual void print() = 0;
};
class I : public Printable {
private:
std::string *name;
public:
I(std::string name);
~I();
void print();
};
I::I(std::string name){
this->name = new std::string {name};
}
I::~I() { delete this->name; }
void I::print(){
std::cout << "The name is: " << *this->name << std::endl;
}
int main
(int argc, char ** argv, char **envp)
{
I me{"Debdut Chakraborty"};
me.print();
return 0;
}
#include <iostream>
#include <ostream>
class I {
private:
std::string *name;
public:
I(std::string name);
~I();
inline friend std::ostream& operator<<(std::ostream& out, const I& me)
{
out << "The string output is: " << *me.name;
return out;
}
};
I::I(std::string name){
this->name = new std::string {name};
}
I::~I() { delete this->name; }
int main
(int argc, char ** argv, char **envp)
{
I me{"Debdut Chakraborty"};
std::cout << me << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment