Introduction to Object-Oriented Programming (OOP) and C++ Basics

Markdown

View as Markdown

Object-Oriented Programming (OOP) is a programming approach that organizes software around objects rather than functions. Unlike procedural programming, where the focus is on writing sequences of instructions, OOP models real-world entities using objects that contain both data and functions.

C++ is one of the most popular object-oriented programming languages. It builds upon the C language by introducing features such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction. These features make it easier to develop large, modular, and maintainable applications.

Learning Objectives

  • Understand Object-Oriented Programming

  • Identify the key features of OOP

  • Understand the structure of a C++ program

  • Write and execute your first C++ program

  • Differentiate between C and C++

  • Apply basic C++ syntax

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm that represents software as a collection of objects.

Each object contains:

  • Data (attributes)

  • Functions (methods)

This approach improves code organization, reusability, and scalability.

Features of Object-Oriented Programming

The four fundamental principles of OOP are:

Encapsulation

Combines data and functions into a single unit called a class.

Abstraction

Displays only the necessary information while hiding implementation details.

Inheritance

Allows one class to inherit the properties and behaviors of another class.

Polymorphism

Allows the same function or operator to behave differently depending on the object.

What is C++?

C++ is a general-purpose programming language developed as an extension of the C language.

It supports:

  • Procedural programming

  • Object-Oriented Programming

  • Generic programming

C++ is widely used in:

  • Software development

  • Game development

  • Operating systems

  • Embedded systems

  • Banking applications

  • Desktop applications

Structure of a C++ Program

A simple C++ program consists of:

  • Header files

  • Namespace declaration

  • main() function

  • Statements

  • Return statement

Example:

#include <iostream>

using namespace std;

int main()

{

    cout << "Welcome to Team Academy";

    return 0;

}

Understanding Each Part

Header File

#include <iostream>

Provides input and output functionality.

Namespace

using namespace std;

Allows access to standard C++ library objects without prefixing std::.

main() Function

Execution begins from:

int main()

Output Statement

cout << "Welcome to Team Academy";

Displays text on the console.

Return Statement

return 0;

Indicates successful program execution.

Compiling a C++ Program

Using the GNU C++ compiler:

g++ hello.cpp -o hello

If compilation succeeds, an executable file is created.

Running the Program

Windows:

.\hello.exe

Output

Welcome to Team Academy

C vs C++

C

C++

Procedural Programming

Object-Oriented Programming

Functions

Classes & Objects

No Inheritance

Supports Inheritance

No Polymorphism

Supports Polymorphism

Limited Reusability

High Code Reusability

Advantages of C++

  • Supports Object-Oriented Programming

  • Code reusability

  • Faster execution

  • Efficient memory management

  • Suitable for large applications

  • Rich Standard Template Library (STL)

Common Mistakes

  • Saving files with the .c extension instead of .cpp

  • Forgetting using namespace std;

  • Missing semicolons

  • Using printf() instead of cout in basic C++ examples

  • Compiling with gcc instead of g++

Best Practices

  • Save C++ programs with the .cpp extension.

  • Use meaningful variable and class names.

  • Format code consistently.

  • Comment complex logic.

  • Compile frequently to identify errors early.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Was this article helpful?

Still need help?

Contact us