fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Vehicle {
  5. public:
  6. Vehicle() {
  7. cout << "Default Konstruktor der Basis-Klasse" << endl;
  8. }
  9.  
  10. Vehicle(int x) {
  11. cout << "param. Konstruktor der Basis-Klasse" << endl;
  12. }
  13. string brand = "Ford";
  14. void honk() {
  15. cout << "Tuut, tuut \n";
  16. }
  17. };
  18.  
  19. class Car : public Vehicle {
  20. public:
  21. Car() : color("red") { // Constructor with initialization list
  22. }
  23. Car(string col) : color(col) {
  24. //_maxSpeed = 200;
  25. cout << "Konstruktor der Car-Klasse" << endl;
  26. setMaxSpeed(200);
  27. }
  28. Car(string col, int s) : color(col) {
  29. //_maxSpeed = 200;
  30. setMaxSpeed(s);
  31. }
  32. string color; // Atributen, Membervariable
  33.  
  34. void drucke() {
  35. cout << "Farbe: " << color << brand << endl;
  36. Vehicle::honk();
  37. }
  38. // getter/setter
  39. void setMaxSpeed(int s) {
  40. if(s < 160)
  41. _maxSpeed = s;
  42. else
  43. _maxSpeed = 160;
  44. }
  45. int getMaxSpeed() {
  46. return _maxSpeed;
  47. }
  48. private:
  49. int _maxSpeed;
  50. };
  51.  
  52. int main() {
  53. Car myObj("blue"); // Erzeugen das Objekt myObj der Klasse Car
  54.  
  55. cout << "Farbe: " << myObj.color << endl;
  56. myObj.color = "red";
  57. myObj.drucke();
  58. printf("Farbe: %s \n", myObj.color.c_str());
  59. myObj.setMaxSpeed(200);
  60. printf("Max Speed: %d \n", myObj.getMaxSpeed());
  61. cout << myObj.brand << endl;
  62. myObj.honk();
  63. return 0;
  64. }
Success #stdin #stdout 0s 4392KB
stdin
Standard input is empty
stdout
Default Konstruktor der Basis-Klasse
Konstruktor der Car-Klasse
Farbe: blue
Farbe: redFord
Tuut, tuut 
Farbe: red 
Max Speed: 160 
Ford
Tuut, tuut