Skip to content

Instantly share code, notes, and snippets.

@MrSimsek
Created September 8, 2019 10:39
Show Gist options
  • Save MrSimsek/0230f0b9af443942863baa45b28d7576 to your computer and use it in GitHub Desktop.
Save MrSimsek/0230f0b9af443942863baa45b28d7576 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
// Normally this is the Car.h file
class Car {
public:
Car();
Car(int hp);
~Car();
// When the properties cannot be modified inside methods we use const
int getHorsePower() const;
void setHorsePower(int hp);
friend void printHorsePower(Car* myCar);
private:
int horsePower;
int* pointerVariable;
};
// Normally this is the Car.cpp file START
// Standard Constructor
Car::Car() : horsePower(180) {
pointerVariable = new int;
*pointerVariable = 0;
std::cout << "Car Class Standard Constructor" << std::endl;
};
// Non-Standard Constructor with parameters
Car::Car(int hp) : horsePower(hp) {
pointerVariable = new int;
*pointerVariable = hp;
std::cout << "Car Class Non-Standard Constructor" << std::endl;
};
// Destructor
Car::~Car() {
delete pointerVariable;
std::cout << "Car Class Destruktor" << std::endl;
};
int Car::getHorsePower() const {
return horsePower;
}
void Car::setHorsePower(int hp) {
horsePower = hp;
};
// Normally this is the Car.cpp file END
void printHorsePower(Car* myCar) {
cout << "The car has " << myCar->horsePower << endl;
}
class Jeep : public Car {
public:
Jeep() {
std::cout << "Jeep Class Standard Constructor" << std::endl;
};
~Jeep() {
std::cout << "Jeep Class Destruktor" << std::endl;
};
};
class Range : public Jeep {
public:
Range() {
std::cout << "Range Class Standard Constructor" << std::endl;
};
~Range() {
std::cout << "Range Class Destruktor" << std::endl;
};
};
int main()
{
std::cout << "START" << std::endl;
//// Creating a static object with Standard constructor
//Car mercedes;
//
//// Setting a horse power value of 210 to an object
//mercedes.setHorsePower(210);
//// To get the horsePower property from a Car object
//mercedes.getHorsePower();
//Car bmw(240);
//bmw.setHorsePower(210);
//bmw.getHorsePower();
//// Creating a dynamic object with Standard constructor
//Car* jaguar = new Car();
//jaguar->getHorsePower();
//jaguar->setHorsePower(300);
//// Creating a dynamic object with Non-Standard constructor
//Car* rolls = new Car(230);
//delete jaguar;
//delete rolls;
//Range ewok;
Car* jaguar = new Car();
printHorsePower(jaguar);
std::cout << "END" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment