DekGenius.com
CPP
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
Inheritance in c++
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
}
};
C++ Inheritance
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut!
" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
C++ inheritance
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut!
" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
C++ Inheritance
class Animal {
// eat() function
// sleep() function
};
class Dog : public Animal {
// bark() function
};
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
inheritance in c++
// C++ program to explain
// Single inheritance
#include<iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle
";
}
};
// sub class derived from a single base classes
class Car : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
C++ Inheritance
class Animal {
// eat() function
// sleep() function
};
class Dog : public Animal {
// bark() function
};
c++ inheritance
#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
";
}
};
int main() {
Dog buddy;
buddy.gender = "male";
buddy.age = 8;
buddy.breed = "husky";
buddy.sound(); // Outputs: Woof
return 0;
}
© 2022 Copyright:
DekGenius.com