Lesson 3 +25 XP

Benefits, Languages & Applications of OOP; Introducing C++

Benefits, Languages & Applications of OOP; Introducing C++

This lesson answers three short-answer favourites: why use OOP (its benefits), which languages support it, and where it is used. It then introduces C++ itself: its history, the structure of a C++ program, and how a program is compiled and run.

1. Benefits of OOP

BenefitHow OOP provides it
ReusabilityInheritance lets a new class reuse an existing class without rewriting it
Data securityData hiding (private) stops outside code from changing data by mistake
ModularityEach object is a self-contained module, so work divides naturally between programmers
Easy maintenanceA change inside one class does not affect code that uses only its interface
ExtensibilityNew classes and features can be added with little change to existing code
Real-world modellingObjects map directly to things in the problem (accounts, students, sensors)
Multiple instancesMany objects of one class can coexist without interfering
Message passingThe interface between objects is simple to describe
Easy upgradeSystems scale from small to large more easily
ProductivityStandard working modules can be combined, instead of writing code from scratch

Limitations (often asked as "disadvantages of OOP"):

  • Programs are usually larger than procedural ones and can run slightly slower.
  • Designing a good class hierarchy needs careful planning and experience.
  • Not every problem fits naturally into objects (for example a short numerical routine).

2. Object Oriented Languages

Languages are grouped by how much of the object model they support.

CategorySupportsExamples
Object-basedencapsulation, objects, data hiding, operator overloading. No inheritance or dynamic bindingAda, early Visual Basic, JavaScript (prototype based)
Object orientedeverything object-based languages have, plus inheritance and dynamic bindingC++, Java, C#, Smalltalk, Python, Eiffel
Object oriented = Object-based + Inheritance + Dynamic binding
LanguageNotes
Smalltalkthe first pure object oriented language; everything is an object
C++adds classes to C; supports procedural and object oriented styles
Javapure OOP except for primitive types; no multiple inheritance of classes
C#Microsoft's .NET language, similar to Java
Pythoneverything is an object; dynamic typing

3. Applications of OOP

OOP is most useful for large, complex systems that change over time:

  1. Real-time systems such as flight control and embedded devices.
  2. Simulation and modelling of traffic, weather or physics.
  3. Object oriented databases that store objects directly.
  4. Hypertext and hypermedia (web pages linking to other objects).
  5. AI and expert systems.
  6. Neural networks and parallel programming.
  7. Decision support and office automation systems.
  8. CAD/CAM (computer-aided design and manufacturing).
  9. Graphical user interfaces, where every window, button and menu is an object.
  10. Games, where every player, enemy and item is an object.

4. The C++ Language

C++ was developed by Bjarne Stroustrup at AT&T Bell Laboratories (New Jersey, USA) in the early 1980s. He wanted the efficiency of C together with the classes of Simula67. The language was first called "C with Classes" and renamed C++ in 1983: ++ is C's increment operator, so the name means "an incremented C".

C++ is a superset of C: almost every C program is also a valid C++ program. The main additions are classes, inheritance, function and operator overloading, references, templates, exceptions and namespaces.

YearMilestone
1979Stroustrup starts "C with Classes"
1983Renamed C++
1985First commercial release; The C++ Programming Language published
1998First ISO standard (C++98)
2011, 2014, 2017, 2020C++11, C++14, C++17, C++20 add modern features

5. A Simple C++ Program

C++
// first.cpp: prints a message
#include <iostream>  // header for cin and cout
using namespace std; // use names from the std namespace

int main() { // execution starts here
    cout << "C++ is better than C." << endl;
    return 0; // 0 tells the system the program succeeded
}

Output:

OUTPUT
C++ is better than C.
PartMeaning
// ...a comment to the end of the line; /* ... */ comments may span lines
#include <iostream>a preprocessor directive that adds the declarations of cin, cout and the stream operators
using namespace std;lets us write cout instead of std::cout (all standard names live in namespace std)
int main()every program starts executing at main; in C++ it returns an int
cout <<cout is the standard output stream; << is the insertion (put-to) operator
endla manipulator that ends the line and flushes the output
return 0;ends main and reports success

Reading input

cin is the standard input stream and >> is the extraction (get-from) operator. Like <<, it can be cascaded.

C++
#include <iostream>
using namespace std;

int main() {
    float a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b; // cascaded extraction
    float sum = a + b;
    float average = sum / 2;
    cout << "Sum = " << sum << ", Average = " << average << endl;
    return 0;
}

Input:

INPUT
6 9

Output:

OUTPUT
Enter two numbers: Sum = 15, Average = 7.5

The input typed by the user (6 9) is not part of the program's output, which is why it does not appear above.

6. Structure of a C++ Program

A typical C++ program is organised in four sections. In larger programs each is often a separate file: class declarations in header files (.h), member function definitions in source files (.cpp), and main in its own file.

Structure of a C++ programStructure of a C++ program

SectionContents
Include files#include directives, using declarations
Class declarationdata members and member function prototypes
Member function definitionsthe bodies of the member functions
Main function programcreates objects and calls their functions
C++
#include <iostream> // 1. include files
using namespace std;

class Person { // 2. class declaration
    char name[30];
    int age;

public:
    void getData(const char* n, int a);
    void display();
};

void Person::getData(const char* n, int a) { // 3. member function definitions
    int i = 0;
    while (n[i] != '\0' && i < 29) {
        name[i] = n[i];
        i++;
    }
    name[i] = '\0';
    age = a;
}
void Person::display() {
    cout << "Name: " << name << ", Age: " << age << endl;
}

int main() { // 4. main function program
    Person p;
    p.getData("Asha", 19);
    p.display();
    return 0;
}

Output:

OUTPUT
Name: Asha, Age: 19

7. Creating, Compiling and Linking

  1. Write the source in an editor and save it with a .cpp extension.
  2. Compile it: the compiler checks syntax and produces an object file (.o or .obj).
  3. Link it: the linker joins the object file with library code (such as iostream) to make an executable (.exe).
  4. Run the executable.
EnvironmentCompile and linkRun
Linux / macOS (GCC)g++ first.cpp -o first./first
Windows (MinGW)g++ first.cpp -o first.exefirst
Turbo C++ (old IDE)Alt+F9 compiles, Ctrl+F9 runsoutput window Alt+F5
ℹ️ Note

Old Turbo C++ code uses #include <iostream.h> and void main() with no using namespace std;. Standard C++ uses #include <iostream>, int main() and the std namespace. Write the standard form in the exam.

Key points to remember

  • Benefits: reusability, data security, modularity, easy maintenance, extensibility, real-world modelling.
  • Object oriented = object-based + inheritance + dynamic binding.
  • C++: Bjarne Stroustrup, AT&T Bell Labs, 1979-1983; first called "C with Classes"; superset of C.
  • cout << is insertion (output); cin >> is extraction (input); both can be cascaded.
  • Program sections: include files, class declaration, member function definitions, main.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. What are the benefits of object oriented programming? Also list its applications. (5 Marks)
  • Write: at least six benefits from the table, the three limitations, and six applications.
  • Q2. Differentiate between object-based and object oriented languages with examples. (2.5 Marks)
  • Write: the formula "object oriented = object-based + inheritance + dynamic binding" and the table.
  • Q3. Explain the structure of a C++ program with an example. (5 Marks)
  • Write: the four sections, the diagram, and the Person program.
  • Q4. Write a short note on the history and features of C++. (2.5 Marks)
  • Q5. Explain cin and cout with the insertion and extraction operators. (2.5 Marks)