Loading lessons...
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
| Benefit | How OOP provides it |
|---|---|
| Reusability | Inheritance lets a new class reuse an existing class without rewriting it |
| Data security | Data hiding (private) stops outside code from changing data by mistake |
| Modularity | Each object is a self-contained module, so work divides naturally between programmers |
| Easy maintenance | A change inside one class does not affect code that uses only its interface |
| Extensibility | New classes and features can be added with little change to existing code |
| Real-world modelling | Objects map directly to things in the problem (accounts, students, sensors) |
| Multiple instances | Many objects of one class can coexist without interfering |
| Message passing | The interface between objects is simple to describe |
| Easy upgrade | Systems scale from small to large more easily |
| Productivity | Standard 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.
| Category | Supports | Examples |
|---|---|---|
| Object-based | encapsulation, objects, data hiding, operator overloading. No inheritance or dynamic binding | Ada, early Visual Basic, JavaScript (prototype based) |
| Object oriented | everything object-based languages have, plus inheritance and dynamic binding | C++, Java, C#, Smalltalk, Python, Eiffel |
| Language | Notes |
|---|---|
| Smalltalk | the first pure object oriented language; everything is an object |
| C++ | adds classes to C; supports procedural and object oriented styles |
| Java | pure OOP except for primitive types; no multiple inheritance of classes |
| C# | Microsoft's .NET language, similar to Java |
| Python | everything is an object; dynamic typing |
3. Applications of OOP
OOP is most useful for large, complex systems that change over time:
- Real-time systems such as flight control and embedded devices.
- Simulation and modelling of traffic, weather or physics.
- Object oriented databases that store objects directly.
- Hypertext and hypermedia (web pages linking to other objects).
- AI and expert systems.
- Neural networks and parallel programming.
- Decision support and office automation systems.
- CAD/CAM (computer-aided design and manufacturing).
- Graphical user interfaces, where every window, button and menu is an object.
- 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.
| Year | Milestone |
|---|---|
| 1979 | Stroustrup starts "C with Classes" |
| 1983 | Renamed C++ |
| 1985 | First commercial release; The C++ Programming Language published |
| 1998 | First ISO standard (C++98) |
| 2011, 2014, 2017, 2020 | C++11, C++14, C++17, C++20 add modern features |
5. A Simple C++ Program
// 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:
C++ is better than C.| Part | Meaning |
|---|---|
// ... | 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 |
endl | a 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.
#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:
6 9Output:
Enter two numbers: Sum = 15, Average = 7.5The 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++ program
| Section | Contents |
|---|---|
| Include files | #include directives, using declarations |
| Class declaration | data members and member function prototypes |
| Member function definitions | the bodies of the member functions |
| Main function program | creates objects and calls their functions |
#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:
Name: Asha, Age: 197. Creating, Compiling and Linking
- Write the source in an editor and save it with a
.cppextension. - Compile it: the compiler checks syntax and produces an object file (
.oor.obj). - Link it: the linker joins the object file with library code (such as
iostream) to make an executable (.exe). - Run the executable.
| Environment | Compile and link | Run |
|---|---|---|
| Linux / macOS (GCC) | g++ first.cpp -o first | ./first |
| Windows (MinGW) | g++ first.cpp -o first.exe | first |
| Turbo C++ (old IDE) | Alt+F9 compiles, Ctrl+F9 runs | output window Alt+F5 |
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.
📝 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
Personprogram. - Q4. Write a short note on the history and features of C++. (2.5 Marks)
- Q5. Explain
cinandcoutwith the insertion and extraction operators. (2.5 Marks)