.h
file
extension.cpp
file
extension.h
file extension#include
#include "myfile.h"
#ifndef UNIQUE_NAME
#define UNIQUE_NAME
...
#endif
.cpp
file extension#ifndef EXAMPLE_FILE_H
#define EXAMPLE_FILE_H
#include <iostream>
#include <string>
class ExampleClass {
public:
int intExample1;
char get_char();
string say_hello(int tmpInt);
private:
char charExample1;
}
#endif //EXAMPLE_FILE_H
#include "myfile.h"
std::string say_hello(int tmpExample) {
return "Hello " + std::to_string(tmpExample);
}
char get_char() {
return charExample1;
}
classDiagram
Dog <|-- GermanShepherd : extends
Dog <|-- GoldenRetriever : extends
Creating classes in C++ is different to Python
Source code is split between two files:
.h
).cpp
)The header contains information about the variables and functions
The source contains the actual implementation of the functions
Classes will be defined using the
class
keyword followed by a name
The body of a class is defined inside a set of
curly brackets ({}
) and terminated by a
semicolon (;
)
class Student {
...
};
public
: members are accessible outside
the classprivate
: members cannot be accessed (or
viewed) outside the classprotected
: members cannot be accessed
outside the class; but can be accessed with
inheritanceclass Lecturer {
public:
std::string name;
private:
int age;
protected:
std::string dateOfBirth;
};
classDiagram
Dog <|-- GermanShepherd : extends
Dog <|-- GoldenRetriever : extends
Lectuer lecturer1;
#include <iostream>
class Lecturer {
public:
std::string name;
private:
int age;
};
int main() {
Lecturer lecturer1;
Lecturer lecturer2;
lecturer1.name = "Ian Cornelius";
lecturer2.name = "Terry Richards";
std::cout << "lecturer1.name -> " << lecturer1.name << std::endl;
std::cout << "lecturer2.name -> " << lecturer2.name << std::endl;
return 0;
}
lecturer1.name -> Ian Cornelius
lecturer2.name -> Terry Richards
class Lecturer {
public:
Lecturer() {
...
}
};
Lecturer
has been created
name
and age
of the lecturerLecturer
has been created
name
and age
of the
lecturer#include <iostream>
class Lecturer {
public:
std::string name;
Lecturer(std::string _name, int _age) {
name = _name;
age = _age;
}
private:
int age;
};
int main() {
Lecturer lecturer1("Ian Cornelius", 34);
Lecturer lecturer2("Terry Richards", -1);
std::cout << "lecturer1.name -> " << lecturer1.name << std::endl;
std::cout << "lecturer2.name -> " << lecturer2.name << std::endl;
return 0;
}
lecturer1.name -> Ian Cornelius
lecturer2.name -> Terry Richards
Lecturer
has been created
name
and age
of the
studentgreeting()
is accessed on
the lecturer1
object#include <iostream>
class Lecturer {
public:
std::string name;
Lecturer(std::string _name, int _age) {
name = _name;
age = _age;
}
std::string greeting() {
return "Hello " + name + ", and welcome to 5062CEM!";
}
private:
int age;
};
int main() {
Lecturer lecturer1("Ian Cornelius", 34);
std::cout << "lecturer1.greeting() -> " << lecturer1.greeting() << std::endl;
return 0;
}
lecturer1.greeting() -> Hello Ian Cornelius, and welcome to 5062CEM!
age
in this class is
privateclass Lecturer {
public:
std::string name;
Lecturer(std::string _name, int _age) {
name = _name;
age = _age;
}
private:
int age;
};
Lecturer
has been created
name
and age
of the
studentget_age()
is accessed on
the lecturer1
object
change_age()
is accessed
on the lecturer
object
#include <iostream>
class Lecturer {
public:
std::string name;
Lecturer(std::string _name, int _age) {
name = _name;
age = _age;
}
int get_age() {
return age;
}
void change_age(int _age) {
age = _age;
}
private:
int age;
};
int main() {
Lecturer lecturer1("Ian Cornelius", 34);
std::cout << "lecturer1.get_age() -> " << lecturer1.get_age() << std::endl;
lecturer1.change_age(-9);
std::cout << "lecturer1.get_age() -> " << lecturer1.get_age() << std::endl;
return 0;
}
lecturer1.get_age() -> 34
lecturer1.get_age() -> -9
:
) characterclass childClass : accessSpecifier parentClass {
...
}
#include <iostream>
// Parent Class
class Person {
public:
std::string name;
Person(std::string _name, int _age) {
name = _name;
age = _age;
}
protected:
int age;
};
// Child Class
class Lecturer : public Person {
public:
using Person::Person;
int get_age() {
return age;
}
void change_age(int _age) {
age = _age;
}
};
int main() {
Lecturer lecturer1("Ian Cornelius", 34);
std::cout << "lecturer1.name -> " << lecturer1.name << std::endl;
std::cout << "lecturer1.get_age() -> " << lecturer1.get_age() << std::endl;
return 0;
}
lecturer1.name -> Ian Cornelius
lecturer1.get_age() -> 34