Created
April 1, 2019 06:16
-
-
Save Mulperi/15cad40d81cce83cf849bd5bbab935b4 to your computer and use it in GitHub Desktop.
C++ classes and inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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