INTERMEDIATE PROGRAMMING
C++
C++ History
C++ How to open OOP
C++ Design class relationship
C++ Inheritance
1ST YEAR BACHELOR OF SCIENCE IN COMPUTER SCIENCE
BERONGOY, MARKJOSEPH, GUEVARA
MS. AVEGEL P. NIEGAS
INTRODUCTION
C++'s File Input and Output (I/O) function allows you to read and write data to system
files. Creating file stream objects and employing suitable methods ('>>', '<<', 'getline()') allows for
flexible and fast interaction with files. To avoid resource leaks, always confirm that files are
successfully opened and shut them after activities. This functionality is critical for activities such
as data logging, storing user input, and handling big datasets. In C++, file I/O enables simple but
powerful file manipulation, giving you control over reading, writing, and manipulating external
data.
RESEARCH
History of C++
The C++ language is an object-oriented programming language & is a combination
of both low-level & high-level language – a Middle-Level Language. The
programming language was created, designed & developed by a Danish Computer
Scientist – Bjarne Stroustrup at Bell Telephone Laboratories (now known as Nokia
Bell Labs) in Murray Hill, New Jersey. As he wanted a flexible & a dynamic
language which was similar to C with all its features, but with additionality of
active type checking, basic inheritance, default functioning argument, classes,
inlining, etc. And hence C with Classes (C++) was launched.
C++ was initially known as “C with classes, ” and was renamed C++ in 1983. ++
is shorthand for adding one to variety in programming; therefore C++ roughly
means that “one higher than C.”
The trace of the programming language C++ can be done back
to 1979 when Bjarne Stroustrup was doing some development for this thesis for
PhD. One in all the words Stroustrup had the chance to figure with, was a language
referred to as Simula, that because the name implies, could be a language primarily
designed for simulations. The Simula sixty-seven language – that was the variant
that Stroustrup worked with is considered the primary language to support the
object-oriented programming paradigm. Stroustrup found that this paradigm was
helpful for package development; but, the Simula language was way too slow for
practice & practical use.
Shortly subsequently, he began work on “C with Classes“, because what the name
implies was meant to be a superset of the C language. His main dream was to get
his advanced object-oriented programming, created by him, into the C language,
which was during his era was still the programming language that was widely
respected for its movability, portability & compactness, by not sacrificing the
speed or the low-level practicality. His programming language enclosed inlining,
basic inheritance, default function arguments, categories, and reliable sort was
checking additionally to all or any the options of the C language.
The first C with categories compiler was referred to as Cfront, that got derived
from a C compiler referred to as CPre. It had been a program designed to translate
C with categories code to universal C. A rather attention-grabbing purpose value
noting is that Cfront was written mostly in C with classes, creating it a self-hosting
compiler (a compiler which will compile itself). Cfront would later be abandoned
in 1993 when it became troublesome to integrate new options into it, mainly C++
exceptions. Even so, Cfront created a significant impact on the implementations of
future compilers and also the operating system UNIX.
In 1983, the name of the language got modified from C with categories to C++.
The ++ operator within the C language is the associate operator for incrementing a
variable, which provides some insight into however Stroustrup regarded the
programming language. Several new options got additional around this point, the
foremost notable of that area unit virtual functions, perform overloading,
references with the const keyword, and single-line comments by using the two
forward slashes (which may be a feature taken from the language BCPL).
In 1985, Stroustrup’s relation to the language entitled The C++ programming
language was printed and got released. That very same year, C++ got enforced as
an advertisement product and hence starting of it as a commercial element. The
programming language wasn’t formally standardized, nevertheless creating the
book an essential reference. The programming language was updated once more in
1989 to incorporate protected and static members, still as an associate inheritance
from many categories & classes.
In 1990, The Annotated C++ manual, which was a reference got released all over.
In 1990 only, in the same & identical year, Borland’s Turbo C++ compiler would
also be commercially discharged as an advertisement product. Turbo C++
additional an excessiveness of other libraries which might have a substantial
impact on C++’s development. Though Turbo C++’s last stable unharness was in
2006, the compiler continues to get widely used.
In 1998, the C++ customary advisory & standards committee printed the first
international standard for C++ ISO/IEC 14882:1998, which might be informally
called C++98. The Annotated C++ manual was aforementioned to be a significant
influence within the development of the quality. The quality template library (also
known as theStandard Template Library) that began its intellectual construction in
1979 got additionally enclosed in it. In 2003, the committee well-versed multiple
issues that were according with their 1998 customary and revised it consequently.
The modified language got dubbed as C++03.
In 2005, the same committee of C++ discharged a technical report (called as TR1)
particularization varied options they were aiming to boost the newest C++
standard. The new rule was informally dubbed C++0x because it was expected to
be discharged someday before the tip of the first decade. Ironically, however, the
new customary wasn’t fulfilled until mid-2011. Many technical reports were
discharged up till then, and a few compilers began adding experimental support for
the new options and features.
In mid-2011, the new C++ customary (dubbed C++11) got finished. TheBoost
libraryproject created a substantial impact on the new rule, and a few of the new
modules were derived directly from the corresponding Boost libraries. A number
of the new options that got included were:
Bjarne Stroustrup has created excellent work with C++. C could be a low-level
programming language, and so, it does not have any classes. It does not contain
several options which will create programming more well-off, however, is that the
quickest language (assembly is more rapid; however, programming in construction
isn’t one thing you’d consider), what Bjarne Stroustrup did he additional the thing
orientated half, by treating code like life objects. What makes C++ therefore lovely
is that it’s the speed of C and it’s additionally a high-level programming language,
therefore allow us to say the most effective of each world. However on the
opposite hand, C++ is challenging to be noted to newbies, and for an equivalent
code that you wrote on C++, you’ll be able to write in Python for the quarter of the
time.
Object Oriented Programming in C++
Object-oriented programming – As the name suggests uses objects in
programming. Object-oriented programming aims to implement real-world entities
like inheritance, hiding, polymorphism, etc. In programming. The main aim of
OOP is to bind together the data and the functions that operate on them so that no
other part of the code can access this data except that function.
There are some basic concepts that act as the building blocks of OOPs i.e.
• Class
• Object
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
Class
The building block of C++ that leads to Object-Oriented programming is a Class. It
is a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class. A
class is like a blueprint for an object. For Example: Consider the Class of Cars.
There may be many cars with different names and brands but all of them will share
some common properties like all of them will have 4 wheels, Speed Limit, Mileage
range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are
their properties.
• A Class is a user-defined data type that has data members and member functions.
• Data members are the data variables and member functions are the functions used
to manipulate these variables together these data members and member functions
define the properties and behavior of the objects in a Class.
• In the above example of class Car, the data member will be speed limit, mileage,
etc and member functions can apply brakes, increase speed, etc
Object
An Object is an identifiable entity with some characteristics and behavior. An
Object is an instance of a Class. When a class is defined, no memory is allocated
but when it is instantiated (i.e. an object is created) memory is allocated.
Objects take up space in memory and have an associated address like a record in
pascal or structure or union. When a program is executed the objects interact by
sending messages to one another. Each object contains data and code to manipulate
the data. Objects can interact without having to know details of each other’s data or
code, it is sufficient to know the type of message accepted and the type of response
returned by the objects.
Encapsulation
In normal terms, Encapsulation is defined as wrapping up data and information
under a single unit. In Object-Oriented Programming, Encapsulation is defined as
binding together the data and the functions that manipulate them. Consider a real-
life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section, etc. The finance section handles all
the financial transactions and keeps records of all the data related to finance.
Similarly, the sales section handles all the sales-related activities and keeps records
of all the sales. Now there may arise a situation when for some reason an official
from the finance section needs all the data about sales in a particular month. In this
case, he is not allowed to directly access the data of the sales section. He will first
have to contact some other officer in the sales section and then request him to give
the particular data. This is what encapsulation is. Here the data of the sales section
and the employees that can manipulate them are wrapped under a single name
“sales section”.
Encapsulation also leads to data abstraction or data hiding. Using encapsulation
also hides the data. In the above example, the data of any of the sections like sales,
finance, or accounts are hidden from any other section.
To know more about encapsulation, refer to this article – Encapsulation in C++
Abstraction
Data abstraction is one of the most essential and important features of object-
oriented programming in C++. Abstraction means displaying only essential
information and hiding the details. Data abstraction refers to providing only
essential information about the data to the outside world, hiding the background
details or implementation. Consider a real-life example of a man driving a car. The
man only knows that pressing the accelerator will increase the speed of the car or
applying brakes will stop the car but he does not know how on pressing the
accelerator the speed is actually increasing, he does not know about the inner
mechanism of the car or the implementation of an accelerator, brakes, etc. In the
car. This is what abstraction is.
• Abstraction using Classes: We can implement Abstraction in C++ using classes.
The class helps us to group data members and member functions using available
access specifiers. A Class can decide which data member will be visible to the
outside world and which is not.
• Abstraction in Header files: One more type of abstraction in C++ can be header
files. For example, consider the pow() method present in math.h header file.
Whenever we need to calculate the power of a number, we simply call the function
pow() present in the math.h header file and pass the numbers as arguments without
knowing the underlying algorithm according to which the function is actually
calculating the power of numbers.
Polymorphism
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than one
form. A person at the same time can have different characteristics. A man at the
same time is a father, a husband, and an employee. So the same person possesses
different behavior in different situations. This is called polymorphism. An
operation may exhibit different behaviors in different instances. The behavior
depends upon the types of data used in the operation. C++ supports operator
overloading and function overloading.
• Operator Overloading: The process of making an operator exhibit different
behaviors in different instances is known as operator overloading.
• Function Overloading: Function overloading is using a single function name to
perform different types of tasks. Polymorphism is extensively used in
implementing inheritance.
Example: Suppose we have to write a function to add some integers, sometimes
there are 2 integers, and sometimes there are 3 integers. We can write the Addition
Method with the same name having different parameters, the concerned method
will be called according to parameters.
Inheritance
The capability of a class to derive properties and characteristics from another class
is called Inheritance. Inheritance is one of the most important features of Object-
Oriented Programming.
• Sub Class: The class that inherits properties from another class is called Sub class
or Derived Class.
• Super Class: The class whose properties are inherited by a sub-class is called
Base Class or Superclass.
• Reusability: Inheritance supports the concept of “reusability”, i.e. when we want
to create a new class and there is already a class that includes some of the code that
we want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.
Dynamic Binding
In dynamic binding, the code to be executed in response to the function call is
decided at runtime. C++ has virtual functions to support this. Because dynamic
binding is flexible, it avoids the drawbacks of static binding, which connected the
function call and definition at build time.
Example:
// C++ Program to Demonstrate the Concept of Dynamic Binding without virtual
function
#include <iostream>
Using namespace std;
Class GFG {
Public:
Void call_Function() // function that calls print
{
Print();
}
Void print() // the display function
{
Cout << “Printing the Base class Content” << endl;
}
};
Class GFG2 : public GFG // GFG2 inherits publicly from GFG
{
Public:
Void print() // GFG2’s display
{
Cout << “Printing the Derived class Content” << endl;
}
};
Int main()
{
GFG* geeksforgeeks = new GFG(); // Creating GFG’s object using pointer
Geeksforgeeks->call_Function(); // Calling call_Function
GFG* geeksforgeeks2 = new GFG2(); // creating GFG2 object using pointer
Geeksforgeeks2->call_Function(); // calling call_Function for GFG2 object
Delete geeksforgeeks;
Delete geeksforgeeks2;
Return 0;
}
Output
Printing the Base class Content Printing the Base class Content
As we can see, the print() function of the parent class is called even from the
derived class object. To resolve this we use virtual functions.
Message Passing
Objects communicate with one another by sending and receiving information. A
message for an object is a request for the execution of a procedure and therefore
will invoke a function in the receiving object that generates the desired results.
Message passing involves specifying the name of the object, the name of the
function, and the information to be sent.
Example;
#include <iostream>
Using namespace std;
// Define a Car class with a method to display its speed
Class Car {
Public:
Void displaySpeed(int speed) {
Cout << “The car is moving at “ << speed << “ km/h.” << endl;
}
};
Int main() {
// Create a Car object named myCar
Car myCar;
// Send a message to myCar to execute the displaySpeed method
Int currentSpeed = 100;
myCar.displaySpeed(currentSpeed);
return 0;
}
//this code is contributed by Md Nizamuddin
Advantage of OOPs over Procedure-oriented programming language
Here are key advantages of Object-Oriented Programming (OOP) over Procedure-
Oriented Programming (POP):
• Modularity and Reusability: OOP promotes modularity through classes and
objects, allowing for code reusability.
• Data Encapsulation: OOP encapsulates data within objects, enhancing data
security and integrity.
• Inheritance: OOP supports inheritance, reducing redundancy by reusing existing
code.
• Polymorphism: OOP allows polymorphism, enabling flexible and dynamic code
through method overriding.
• Abstraction: OOP enables abstraction, hiding complex details and exposing only
essential features
C++ Class Relationships
The C++ classes are related to each other in two ways.
The first is the Base-Derived relationship
The F90 API supports the Base-Derived relationships through Base-Class
conversion functions. These are: block, attributable, and labelled.
And the second is the Parent-Child relationship.
These relationships are handled in the F90 API through the parent() function. This
function has been overloaded, and returns the parent object of the given object.
The following table shows this simple relationship:
Inheritance
In C++, it is possible to inherit attributes and methods from one class to another.
We group the “inheritance concept” into two categories:
• derived class (child) – the class that inherits from another class
• base class (parent) – the class being inherited from
To inherit from a class, use the : symbol.
In the example below, the Car class (child) inherits the attributes and methods from
the Vehicle class (parent):
Example
// Base class
Class Vehicle {
Public:
String brand = “Ford”;
Void honk() {
Cout << “Tuut, tuut! \n” ;
}
};
// Derived class
Class Car: public Vehicle {
Public:
String model = “Mustang”;
};
Int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + “ “ + myCar.model;
return 0;
}
Output
Tuut, tuut!
Ford Mustang
SUMMARY
The C++ language is an object-oriented programming language & is a combination of
both low-level & high-level language – a Middle-Level Language. The programming language
was created, designed & developed by a Danish Computer Scientist – Bjarne Stroustrup at Bell
Telephone Laboratories (now known as Nokia Bell Labs) in Murray Hill, New Jersey.
Object-oriented programming – As the name suggests uses objects in programming. Object-
oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism, etc. In programming.
The C++ classes are related to each other in two ways.
The first is the Base-Derived relationship
The F90 API supports the Base-Derived relationships through Base-Class conversion functions.
These are: block, attributable, and labelled. And the second is the Parent-Child relationship. In
C++, it is possible to inherit attributes and methods from one class to another. We group the
“inheritance concept” into two categories:
REFERENCES
https://www.geeksforgeeks.org/history-of-c/
https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/
https://xmm-tools.cosmos.esa.int/external/sas/current/doc/dal/node21.html