C++ Base and Derived Classes šŸŽÆ

beginner
9 min

C++ Base and Derived Classes šŸŽÆ

Welcome to our deep dive into C++ Base and Derived Classes! In this comprehensive lesson, we'll explore the intricacies of these powerful tools that will help you write more efficient, reusable, and easier-to-manage code.

Before we dive in, let's quickly recap what we've learned so far in C++:

  • Variables: The fundamental building blocks of any program.
  • Data Types: Including int, float, char, bool, and more.
  • Operators: Arithmetic, assignment, comparison, and logical operators.
  • Control Structures: If-else, loops (for, while, do-while).
  • Functions: Defining reusable pieces of code.

Now, it's time to take our knowledge to the next level with classes and objects!

What are Classes and Objects in C++? šŸ“

Classes are user-defined data types that encapsulate data and functions together. They provide a blueprint for creating objects, which are instances of a class.

Here's a simple example of a class named Person:

cpp
#include <iostream> using namespace std; class Person { public: string name; int age; }; int main() { Person john; // Creating an object 'john' of class Person john.name = "John Doe"; john.age = 30; cout << "Name: " << john.name << ", Age: " << john.age << endl; return 0; }

In this example, we defined a Person class with two public members: name and age. We then created an object john of type Person and assigned values to its properties.

Base and Derived Classes šŸ’”

A derived class is a class that inherits properties and behaviors from another class, known as the base class. Derived classes can extend, modify, or override the properties and methods of the base class.

Let's illustrate this with an example:

cpp
#include <iostream> using namespace std; // Base Class class Animal { public: string sound; void makeSound() { cout << "Animal Sound" << endl; } }; // Derived Class class Dog : public Animal { public: string breed; void makeSound() { cout << "Woof Woof" << endl; } }; int main() { Dog myDog; myDog.breed = "German Shepherd"; myDog.makeSound(); // Output: Woof Woof return 0; }

In this example, we have a base class Animal with a sound property and a makeSound method. We then created a derived class Dog that inherits from Animal. The Dog class has an additional breed property and overrides the makeSound method to output a specific sound for dogs.

Inheritance and Member Access Specifiers šŸ“

Inheritance allows a derived class to inherit properties and behaviors from a base class. The access specifiers public, private, and protected are used to control the visibility and accessibility of these inherited members.

Here's an example demonstrating the use of these access specifiers:

cpp
#include <iostream> using namespace std; class Base { private: int privateBase; protected: int protectedBase; public: int publicBase; }; class Derived : public Base { public: void printBaseValues() { cout << "Private Base Value: " << this->privateBase << endl; // Accessible cout << "Protected Base Value: " << this->protectedBase << endl; // Accessible cout << "Public Base Value: " << this->publicBase << endl; // Accessible } }; int main() { Derived derivedObj; derivedObj.publicBase = 10; // Accessible derivedObj.printBaseValues(); return 0; }

In this example, we have a base class Base with three integer members: privateBase, protectedBase, and publicBase. We created a derived class Derived that inherits from Base.

Since privateBase is marked as private, it can only be accessed within the Base class. protectedBase can be accessed within the Base class and its derived classes but not outside of them. publicBase can be accessed from anywhere, including the derived class and the outside world.

Polymorphism šŸ’”

Polymorphism is the ability of an object to take on many forms. In C++, polymorphism can be achieved using function overloading, operator overloading, and runtime binding.

Here's an example demonstrating runtime binding (dynamic binding) using virtual functions:

cpp
#include <iostream> using namespace std; class Animal { public: virtual void makeSound() { cout << "Animal Sound" << endl; } }; class Dog : public Animal { public: void makeSound() { cout << "Woof Woof" << endl; } }; class Cat : public Animal { public: void makeSound() { cout << "Meow Meow" << endl; } }; int main() { Animal* animals[2]; animals[0] = new Dog(); animals[1] = new Cat(); for(int i = 0; i < 2; i++) { animals[i]->makeSound(); } return 0; }

In this example, we have a base class Animal with a makeSound method marked as virtual. We then created two derived classes: Dog and Cat. Both derived classes override the makeSound method to output specific sounds for each animal.

During runtime, the appropriate derived class's makeSound method is called based on the actual object being pointed to by the Animal* pointer. This is known as dynamic binding or runtime binding.

Quick Quiz
Question 1 of 1

What is the difference between a base class and a derived class in C++?

That's it for our deep dive into C++ Base and Derived Classes! We've covered the basics of classes, objects, inheritance, member access specifiers, and polymorphism. With this knowledge, you're well on your way to mastering C++ and creating powerful, efficient, and reusable code.

Happy coding, and remember to stay curious, experiment, and have fun while learning! 🌟