Skip to content

Instantly share code, notes, and snippets.

@Mulperi
Created April 1, 2019 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mulperi/15cad40d81cce83cf849bd5bbab935b4 to your computer and use it in GitHub Desktop.
Save Mulperi/15cad40d81cce83cf849bd5bbab935b4 to your computer and use it in GitHub Desktop.
C++ classes and inheritance
// Example program
#include <iostream>
#include <string>
class Human {
std::string name;
public: // Without this, methods are set private and cannot be accessed outside the class
Human(std::string name = "Anonymous"){this->setName(name);}
std::string getName() {return this->name;};
void setName(std::string name) {this->name = name;};
};
class Programmer: public Human {
public:
Programmer(std::string name = "haxor"): Human(name) {} // Calling super constructor with argument
};
int main()
{
Programmer p;
p.setName("Jorma");
std::cout << p.getName() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment