xxxxxxxxxx
class A {
//statements of class A
};
class B:public A { //inherting class B from class A
//statements of class B
};
xxxxxxxxxx
using namespace std;
#include <string>
class Cat {
public:
string name;
Cat() {
this->name = "Garfield";
}
string sound(){ //implemented sound method from the interface
return "Meow!";
}
};
class Tiger : public Cat { //the variable 'name' and the method 'sound()' are inherited
public:
Tiger() : Cat(){
//calling the constructor in the base class, Cat
}
string sound(){ //overwriting the sound() method in Cat
return "Roar!";
}
string catSound() : Cat{
return Cat::sound(); //calling the sound method from Cat, non-overwritten
}
};
xxxxxxxxxx
#include <iostream>
// Base class
class Animal {
public:
std::string gender;
int age;
};
// Derived class
class Dog: public Animal {
public:
std::string breed;
void sound() {
std::cout << "Woof\n";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
xxxxxxxxxx
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
xxxxxxxxxx
#include <iostream>
// Base class
class Animal {
public:
std::string gender;
int age;
};
// Derived class
class Dog: public Animal {
public:
std::string breed;
void sound() {
std::cout << "Woof\n";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
xxxxxxxxxx
#include <iostream>
// Base class
class Animal {
public:
std::string gender;
int age;
};
// Derived class
class Dog: public Animal {
public:
std::string breed;
void sound() {
std::cout << "Woof\n";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
xxxxxxxxxx
#include <iostream>
// Base class
class Animal {
public:
std::string gender;
int age;
};
// Derived class
class Dog: public Animal {
public:
std::string breed;
void sound() {
std::cout << "Woof\n";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
xxxxxxxxxx
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
xxxxxxxxxx
#include <iostream>
// Base class
class Animal {
public:
std::string gender;
int age;
};
// Derived class
class Dog: public Animal {
public:
std::string breed;
void sound() {
std::cout << "Woof\n";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
xxxxxxxxxx
#include <iostream>
// Base class
class Animal {
public:
std::string gender;
int age;
};
// Derived class
class Dog: public Animal {
public:
std::string breed;
void sound() {
std::cout << "Woof\n";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}