Introduction to OOP in C++
Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 5 of 104.
Object-Oriented Programming organizes code into classes that bundle data and behavior together.
A simple class with public members
class Dog {
public:
std::string name;
int age;
std::string bark() {
return name + " says Woof!";
}
};Creating and using an object
Dog dog;
dog.name = "Buddy";
dog.age = 3;
std::cout << dog.bark() << std::endl;Output:
Buddy says Woof!A class defines the structure — what data it holds and what actions it can perform. An object is a specific instance you create and use. Members under public: can be accessed from anywhere.
Challenge
EasyCreate a Dog class with public members and methods:
- Public members:
name(string) andage(int) bark()— returns"<name> says Woof!"info()— returns"<name> is <age> years old"
Cheat sheet
Object-Oriented Programming organizes code into classes that bundle data and behavior together.
A class defines the structure — what data it holds and what actions it can perform. An object is a specific instance you create and use.
Defining a class with public members:
class Dog {
public:
std::string name;
int age;
std::string bark() {
return name + " says Woof!";
}
};Creating and using an object:
Dog dog;
dog.name = "Buddy";
dog.age = 3;
std::cout << dog.bark() << std::endl;
// Output: Buddy says Woof!Members under public: can be accessed from anywhere.
Try it yourself
#include <iostream>
#include "Dog.h"
int main() {
std::string name;
int age;
std::getline(std::cin, name);
std::cin >> age;
Dog dog;
dog.name = name;
dog.age = age;
std::cout << dog.bark() << std::endl;
std::cout << dog.info() << std::endl;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesC++ Build & CompilationHeader Files & Source FilesNamespaces & ScopeIntroduction to OOP in C++Classes vs ObjectsThe 'this' PointerMethods (Member Functions)Attributes (Data Members)Ctors & Dtors BasicsRecap - Simple Calculator4Class Properties
Instance vs Static MembersGetters and SettersConst Member FunctionsMutable KeywordStatic Methods and VariablesFriend Functions & ClassesRecap - Bank Account Manager7Inheritance
Basic InheritanceInheritance Access LevelsCtor & Dtor Call OrderMethod OverridingVirtual Functions & VTableMultiple InheritanceVirtual InheritanceRecap - Employee Hierarchy2Memory Management
Stack vs Heap MemoryPointers and ReferencesDynamic Memory (new/delete)Smart Pointers in C++RAII in C++Recap - Dynamic Array Manager5Encapsulation
Access Specifiers in C++Access Specifiers In DepthInformation HidingStruct vs ClassNested & Inner ClassesRecap - Student Records System8Polymorphism
Compile vs Runtime PolymorphFunction OverloadingVirtual Functions RevisitedPure Virtual FunctionsAbstract ClassesInterface Design in C++Dynamic Casting & RTTIRecap - Shape Calculator3Constructors & Destructors
Default ConstructorParameterized ConstructorCopy ConstructorMove ConstructorConstructor Init ListsDelegating ConstructorsDestructor Deep DiveRule of Three / Five / ZeroRecap - String Class6Operator Overloading
Intro to Operator OverloadArithmetic Operator OverloadComparison Operator OverloadStream OperatorsAssignment Operator Overload[] and () Operator OverloadType Conversion OperatorsRecap - Matrix Class9Templates
Function TemplatesClass TemplatesTemplate SpecializationVariadic TemplatesSFINAE & Type Traits BasicsRecap - Generic Container