Created
April 5, 2013 21:56
-
-
Save jonathanmarvens/5322978 to your computer and use it in GitHub Desktop.
A simple C++ class example I used for teaching a few friends C++.
This file contains hidden or 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
#include <cctype> | |
#include <iostream> | |
class Person | |
{ | |
public: | |
std::string getNameFirst(); | |
std::string getNameLast(); | |
void setNameFirst( std::string value ); | |
void setNameLast( std::string value ); | |
Person( std::string name_first, std::string name_last ); | |
~Person(); | |
private: | |
std::string name_first; | |
std::string name_last; | |
std::string capitalizeFirstLetter( std::string value ); | |
}; | |
Person::Person( std::string name_first, std::string name_last ) | |
{ | |
this->setNameFirst( name_first ); | |
this->setNameLast( name_last ); | |
} | |
Person::~Person() | |
{} | |
std::string Person::getNameFirst() | |
{ | |
return this->name_first; | |
} | |
std::string Person::getNameLast() | |
{ | |
return this->name_last; | |
} | |
void Person::setNameFirst( std::string value ) | |
{ | |
this->name_first = this->capitalizeFirstLetter( value ); | |
} | |
void Person::setNameLast( std::string value ) | |
{ | |
this->name_last = this->capitalizeFirstLetter( value ); | |
} | |
std::string Person::capitalizeFirstLetter( std::string value ) | |
{ | |
value[ 0 ] = toupper( value[ 0 ] ); | |
return value; | |
} | |
int main( int arguments_count, char ** arguments ) | |
{ | |
std::string name_first; | |
std::string name_last; | |
name_first = ( arguments_count >= 3 ) ? arguments[ 1 ] : "foo"; | |
name_last = ( arguments_count >= 3 ) ? arguments[ 2 ] : "bar"; | |
Person * person = new Person( name_first, name_last ); | |
std::cout << "First name: " << person->getNameFirst() << std::endl; | |
std::cout << "Last name: " << person->getNameLast() << std::endl; | |
delete person; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment