C++ classes
How the files are organized
You have come up with a class for bicycles - Bicycle.
You need to declare the class and all its private and public variables
in the header file
Bicycle.h
All methods and other logic need to go in the file
Bicycle.cpp
Suppose that in the file
Bicycle.h
you have declared the getManuf() method, which
should return the manufacturer name of a particular bicycle model.
#include <string>
class Bicycle
{
private:
std::string modelName;
std::string manufName;
int yearManuf;
public:
Bicycle(std::string name, std::string manuf, int year);
Bicycle();
std::string getManuf();
};
In the file Bicycle.cpp you will need to include Bicycle.h
#include "Bicycle.h"
When declaring the function you need to specify that it belongs to the Bicycle class
std::string Bicycle::getManuf()
{
return "Manufacturer name is " + manufName;
}
The code on this page was run with g++ 15.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Andrei Olegovich