classes
This commit is contained in:
9
cpp/classes/README.md
Normal file
9
cpp/classes/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Classes in C++
|
||||
|
||||
In c++ classes are defined using a header file and the implementation in a
|
||||
regular .cpp file. Explanations to the structure can be found in the files themselves
|
||||
|
||||
## Relevant files
|
||||
|
||||
- [car.h](./car.h)
|
||||
- [car.cpp](./car.cpp)
|
23
cpp/classes/car.cpp
Normal file
23
cpp/classes/car.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
// include the header file
|
||||
#include "car.h"
|
||||
|
||||
// include necessary libraries and namespaces
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// define the implementation for car.print_info()
|
||||
void car::print_info()
|
||||
{
|
||||
cout << "Car Information:" << endl;
|
||||
// fields that are defined in the header file can just be used like normal variables here
|
||||
cout << "- manufacturer: " << manufacturer << endl;
|
||||
cout << "- model: " << model << endl;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
That's everything for the .cpp file.
|
||||
Since fields don't have an implementation and are already defined in the header file, they can be left out here.
|
||||
|
||||
*/
|
40
cpp/classes/car.h
Normal file
40
cpp/classes/car.h
Normal file
@ -0,0 +1,40 @@
|
||||
// #pragma once is usually included in header files to make sure they are only loaded once
|
||||
// it's a simpler version of include guards, which are necessary to prevent conflicts
|
||||
#pragma once
|
||||
/*
|
||||
|
||||
in case #pragma once is not supported by your compiler you'll need more basic include guards
|
||||
in this case it would be
|
||||
#ifndef car_h
|
||||
#define car_h
|
||||
|
||||
...class contents
|
||||
|
||||
#endif
|
||||
|
||||
*/
|
||||
|
||||
// including a necessary library and namespace to work with strings
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// define the class
|
||||
class car
|
||||
{
|
||||
// define the visibility of the following fields and methods
|
||||
// in this case everything is public since visibility is a topic in future lessons
|
||||
public:
|
||||
// define two strings, manufacturer and model
|
||||
std::string manufacturer;
|
||||
std::string model;
|
||||
|
||||
// define a function that will print out information
|
||||
void print_info();
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
That completes the header file, as you can see, there are no implementations in here.
|
||||
That's because they will be added in the car.cpp file.
|
||||
|
||||
*/
|
20
cpp/classes/main.cpp
Normal file
20
cpp/classes/main.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
#include "car.h"
|
||||
#include <cstring>
|
||||
|
||||
int main() {
|
||||
// create a new car and store it in the variable 'a'
|
||||
// in contrast to java, c++ does not need initialization with the 'new' keyword here.
|
||||
car a;
|
||||
// set some data for that car
|
||||
a.manufacturer = "Benz";
|
||||
a.model = "Velo";
|
||||
|
||||
// do the same for a second car
|
||||
car b;
|
||||
b.manufacturer = "Ford";
|
||||
b.model = "Model T";
|
||||
|
||||
// use a function of the car class to print out the information
|
||||
a.print_info();
|
||||
b.print_info();
|
||||
}
|
Reference in New Issue
Block a user