+ 4
basic C++ programs are usually consisted of .cpp files and .h files .h are header files which define the class' attributes, methods, various includes which the class depends on (header file for each class) .cpp file for each of the classes for methods implementation .cpp file for the main(). from there the program will start. for example a class which represent a car: Car class ---------------- Car.h ~~~~~~~~~~~~~~~ #include<iostream> #include<string> using namespace std; class Car{ private: string name; string color; int year; public: // constructor Car(string name, string color, int year); // destructor ~Car(); void print(); } ~~~~~~~~~~~~~~~ Car.cpp ~~~~~~~~~~~~~~~ #include "Car.h" // link to the header file Car::Car(string name, string color, int year){ this->name = name; this->color = color; this->year = year; } Car::~Car(){ cout << "Car object destroyed" << endl; } void Car::print(){ cout << "Car name: " << this->name << endl; cout << "Car color: " << this->color << endl; cout << "Car manufacture year: " << this->year << endl; } ~~~~~~~~~~~~~~~ main.cpp ~~~~~~~~~~~~~~~ #include "Car.h" int main(){ Car obj("Chevrolet Corvette", "Red", 1969); obj.print(); // call to print from Car.cpp file return 0; } ~~~~~~~~~~~~~~~ i will let you complete the output of this program :)
20th Nov 2016, 7:04 PM
Burey
Burey - avatar