Introduction to OOP and C++ Fundamentals

SA
StudyAI Editorial
Reviewed by StudyAI tutors
· Published Updated

From the Object oriented programming using c++ curriculum

Introduction to OOP and C++ Fundamentals

TL;DR

Object-Oriented Programming (OOP) helps organize your code into reusable "objects" that model real-world things. C++ is a powerful programming language that fully supports OOP, allowing you to write efficient and structured programs. You'll learn fundamental C++ concepts like variables, data types, and basic input/output to start building your first programs.

1. The Mental Model

Think of OOP like building with LEGOs. Instead of crafting every single brick from scratch for each project, you create specialized, reusable blocks (objects) that have their own properties and behaviors. C++ is the workbench and tools you'll use to design and assemble these LEGO creations.

2. The Core Material

When you're programming, you're essentially giving instructions to a computer. C++ is a language that helps you do this very precisely and efficiently. It's built on a foundation of procedural programming but adds powerful OOP features to help you manage complex projects.

What is OOP?

A low-angle shot of a rusty basketball hoop and backboard set against a vibrant blue sky.
Photo by Furkan Dolunay on Pexels

OOP is a programming paradigm that organizes software design around data, or objects, rather than functions and logic. It focuses on breaking down a programming problem into smaller, manageable pieces by creating objects. Each object is an instance of a class, which acts like a blueprint.

There are four main pillars of OOP:

  • Encapsulation: Bundling data (attributes) and the methods (functions) that operate on the data into a single unit, or class. It also restricts direct access to some of an object's components, which is called "information hiding."
  • Abstraction: Showing only essential attributes and hiding unnecessary details. Think of a car; you know how to drive it (accelerate, brake), but you don't need to know the intricate details of its engine to operate it.
  • Inheritance: A mechanism where one class (child/derived class) can acquire the properties and behaviors of another class (parent/base class). This promotes code reusability.
  • Polymorphism: The ability of an object to take on many forms. For example, a "shape" object could be drawn as a circle, square, or triangle, each having its own specific drawing method but invoked through a common interface.
graph TD
    A["Object-Oriented Programming (OOP)"] --> B["Encapsulation"];
    A --> C["Abstraction"];
    A --> D["Inheritance"];
    A --> E["Polymorphism"];
    B --> B1["Bundles Data & Methods"];
    B --> B2["Information Hiding"];
    C --> C1["Shows Essentials"];
    C --> C2["Hides Complexity"];
    D --> D1["Code Reusability"];
    D --> D2["Parent/Child Classes"];
    E --> E1["Many Forms"];
    E --> E2["Common Interface"];

C++ Fundamentals: Getting Started

Scrabble tiles spelling 'START' against a pink background. Perfect for motivational themes.
Photo by İdil Ceren Çelikler on Pexels

Let's look at the basic building blocks in C++.

Variables and Data Types

Variables are named storage locations that can hold data. Every variable has a data type, which tells the compiler what kind of data it will store (e.g., numbers, text) and how much memory to reserve.

Here are some common C++ data types:

  • int: Stores whole numbers (e.g., 5, -100).
  • double: Stores floating-point numbers (numbers with decimals, e.g., 3.14, -0.5).
  • char: Stores a single character (e.g., 'A', 'b', '7').
  • bool: Stores boolean values (true or false).
  • std::string: Stores sequences of characters (text, e.g., "Hello World"). This isn't a built-in type but part of the standard library.
#include <iostream> // For input/output operations
#include <string>   // For using std::string

int main() {
    // Declaring and initializing variables
    int age = 30;
    double price = 19.99;
    char grade = 'A';
    bool isActive = true;
    std::string name = "Alice";

    // You can also declare first, then assign later
    int quantity;
    quantity = 100;

    // Outputting variable values
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Price: " << price << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Is Active: " << isActive << std::endl; // true usually prints as 1, false as 0
    std::cout << "Quantity: " << quantity << std::endl;

    return 0; // Indicates successful program execution
}

When you run this code, it'll print the values you assigned to each variable. Notice std::cout is used for output, << is the insertion operator, and std::endl moves to a new line.

Input and Output (I/O)

You've seen std::cout for output. To get input from the user, you'll use std::cin along with the extraction operator >>.

#include <iostream>
#include <string>

int main() {
    std::string yourName;
    int yourAge;

    std::cout << "Enter your name: ";
    std::cin >> yourName; // Reads a single word

    std::cout << "Enter your age: ";
    std::cin >> yourAge;

    std::cout << "Hello, " << yourName << "! You are " << yourAge << " years old." << std::endl;

    return 0;
}

When you run this, it'll prompt you to type in your name and age, then greet you. Be aware that std::cin >> yourName; only reads up to the first space. If you want to read an entire line including spaces, you'd use std::getline(std::cin, yourName);.

3. Worked Example

Let's combine what we've learned to create a simple program that calculates the area of a rectangle based on user input.

#include <iostream> // Required for std::cout and std::cin

int main() {
    // 1. Declare variables to store width, height, and area
    double width;
    double height;
    double area;

    // 2. Prompt the user for the width
    std::cout << "Please enter the width of the rectangle: ";
    // 3. Read the width from user input
    std::cin >> width;

    // 4. Prompt the user for the height
    std::cout << "Please enter the height of the rectangle: ";
    // 5. Read the height from user input
    std::cin >> height;

    // 6. Calculate the area
    area = width * height;

    // 7. Display the result to the user
    std::cout << "A rectangle with width " << width
              << " and height " << height
              << " has an area of " << area << "." << std::endl;

    return 0; // Indicate successful program termination
}

When you run this, it'll interact with you:
Please enter the width of the rectangle: 5.5 (you type this)
Please enter the height of the rectangle: 3.0 (you type this)
A rectangle with width 5.5 and height 3 has an area of 16.5. (program outputs this)

This example shows how to declare variables, take user input, perform a calculation, and display the result.

4. Key Takeaways

  • OOP helps organize your code into modular, reusable components called objects.
  • The four pillars of OOP are Encapsulation, Abstraction, Inheritance, and Polymorphism.
  • C++ uses int, double, char, bool, and std::string for common data types.
  • std::cout is for printing output to the console, and std::cin is for reading user input.
  • main() is the entry point for every C++ program, and return 0; signals successful execution.

Common mistakes to avoid:
- Forgetting to include necessary headers like <iostream> or <string>.
- Using std::cin to read a sentence with spaces; it will only capture the first word.
- Mixing up data types (e.g., trying to put text into an int variable).
- Not declaring variables before you try to use them.

5. Now Try It

Write a C++ program that asks the user for their favorite color, then their lucky number. After receiving both inputs, the program should print a friendly message like: "Your favorite color is [color] and your lucky number is [number]! What a great combination!"

What success looks like: Your program compiles without errors, prompts the user correctly, and then displays the personalized message using the color and number they provided.

Frequently asked about Introduction to OOP and C++ Fundamentals

Object-Oriented Programming (OOP) helps organize your code into reusable "objects" that model real-world things. C++ is a powerful programming language that fully supports OOP, allowing you to write efficient and structured programs. Read the full notes above for the details.

Introduction to OOP and C++ Fundamentals is a core topic in Object oriented programming using c++. Most exam papers test it via a mix of definitions, worked examples, and applied problems. The notes above cover the high-yield sub-topics, common pitfalls, and the kind of questions examiners typically set.

Yes — every note in the StudyAI Campus Hub is free to read in full, right here on this page, with no account needed. If you clone the plan into your own dashboard, the free plan shows a preview of each note there; Basic and above unlock the full notes in your dashboard, along with practice quizzes, flashcards and offline study. You can always come back here to read the complete note for free.

Study this next


Get the full Object oriented programming using c++ curriculum

Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.

Create Free Account