0% found this document useful (0 votes)
33 views24 pages

Object-Oriented Programming in C++

The document provides an overview of Object-Oriented Programming (OOP) and its implementation in C++, highlighting key concepts such as classes, objects, and the four pillars of OOP: inheritance, polymorphism, encapsulation, and abstraction. It explains the structure of a C++ program, including the use of comments, namespaces, and the main function, while emphasizing the importance of understanding programming concepts for effective system design and maintenance. Additionally, it outlines the advantages of OOP over procedural programming and the various applications of C++ in software development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views24 pages

Object-Oriented Programming in C++

The document provides an overview of Object-Oriented Programming (OOP) and its implementation in C++, highlighting key concepts such as classes, objects, and the four pillars of OOP: inheritance, polymorphism, encapsulation, and abstraction. It explains the structure of a C++ program, including the use of comments, namespaces, and the main function, while emphasizing the importance of understanding programming concepts for effective system design and maintenance. Additionally, it outlines the advantages of OOP over procedural programming and the various applications of C++ in software development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MBEYA UNIVERSITY OF SCIENCE AND

TECHNOLOGY
INSTITUTE OF SCIENCE AND TECHNOLOGY
DEPARTMENT OF INFORMATICS

ORDINARY DIPLOMA IN ELECTRICAL AND ELECTRONICS


ENGINEERING/ ELECTRONICS AND TELECOMMUNICATION
UQF 6 – SECOND YEAR
COT 6202: OBJECT-ORIENTED PROGRAMMING (OOP)

No. of Credit: 07
Object-Oriented Programming (OOP)
• What is OOP?
• Is a programming paradigm that represents concept as
“objects” that have data fields (attributes that describe the
object) and associated procedures known as methods
• In OOP languages like C++, the data and functions
(procedures to manipulate the data) are bundled together as a
self-contained unit called an object.
OOP cont..
• In C++ programming language , class describes both the
properties (data) and behaviors (functions) of the objects.
• A Class : is a way to bind the data and its associated
functions together. All the elements of a class are private by
default, even though elements can be declared as public or
protected. An object is an instance of a class.
class object
Fruit Apple
Banana
Mango

Car Volvo
Audi
Toyota
Object-oriented programming has several advantages over procedural programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code and
shorter development time
The "Don't Repeat Yourself" (DRY) principle is about reducing the
repetition of code. You should extract out the codes that are common
for the application, and place them at a single place and reuse them
instead of repeating it.
C++ Programming Language

What is C++?
What is C++?
• C++ is a general purpose, case-sensitive, free-form programming
language that supports object-oriented, procedural and generic
programming.
• C++ inherits most of C's syntax and the C preprocessor.
• C++ supports the object-oriented programming, the four major pillar of
object-oriented programming (OOPs) used in C++ are:
 Inheritance
 Polymorphism
 Encapsulation
 Abstraction
• Abstraction: Hiding internal details and showing functionality is
known as abstraction. For example: phone call, we don't know the
internal processing.
• Encapsulation: Binding (or wrapping) code and data together into a single unit
is known as encapsulation. For example: capsule, it is wrapped with
different medicines.
• Inheritance: When one object acquires all the properties and behaviours
of parent object i.e. known as inheritance. It provides code reusability.
• Polymorphism: means many forms. It is the ability to take more
than one form. It is a feature that provides a function or an
operator with more than one definition.
• Objects - C++ introduces object-oriented (OO) features to C.
C++ Standard Libraries
Standard C++ consists of three important parts –
• The core library includes the data types, variables and literals, etc.
• The standard library includes the set of functions manipulating
strings, files, etc.
• The Standard Template Library (STL) includes the set of methods
manipulating a data structure.
Learning C++
• The most important thing while learning C++ is to focus on
concepts.
• The purpose of learning a programming language is to become a
better programmer; that is, to become more effective at designing
and implementing new systems and at maintaining old ones.
• C++ supports a variety of programming styles. You can write in the
style of Fortran, C, Smalltalk, etc., in any language. Each style can
achieve its aims effectively while maintaining runtime and space
efficiency.
Use of C++
• C++ is used by hundreds of thousands of programmers in
essentially every application domain.
• C++ is being highly used to write device drivers and other software
that rely on direct manipulation of hardware under real-time
constraints.
• C++ is widely used for teaching and research because it is clean
enough for successful teaching of basic concepts.
• Anyone who has used either an Apple Macintosh or a PC running
Windows has indirectly used C++ because the primary user
interfaces of these systems are written in C++
C++ Basic Syntax
• When we consider a C++ program, it can be defined as a
collection of objects that communicate via invoking each other's
methods. Let us now briefly look into what a class, object,
methods, and instant variables mean.
• Object − Objects have states and behaviors. Example: A dog
has states - color, name, breed as well as behaviors - wagging,
barking, eating. An object is an instance of a class.
• Class − A class can be defined as a template/blueprint that
describes the behaviors/states that object of its type support.
• Methods − A method is basically a behavior. A class can contain
many methods. It is in methods where the logics are written, data
is manipulated and all the actions are executed.
• Instance Variables − Each object has its unique set of instance
variables. An object's state is created by the values assigned to
these instance variables.
Structure of a program
// my first program in C++
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
Structure of a program
// my first program in C++
• This is a comment line. All lines beginning with two slash
signs (//) are considered comments and do not have any
effect on the behavior of the program. The programmer
can use them to include short explanations or observations
within the source code itself. In this case, the line is a brief
description of what our program is.
Comments
• Comments are simply text that is ignored by the
compiler, but that may inform the reader of what you are
doing at any particular point in your program.
• C++ comments come in two flavors:
 the double-slash (//) comment, and
 the slash-star (/*) comment.
Comments cont…
• The double-slash comment, which will be referred to as a C++-
style comment, tells the compiler to ignore everything that follows
this comment, until the end of the line.
• The slash-star comment mark tells the compiler to ignore
everything that follows until it finds a star-slash (*/) comment
mark.
• These marks will be referred to as C-style comments. Every /*
must be matched with a closing */.
Structure of a program
#include <iostream>
• Lines beginning with a hash sign (#) are directives for the
preprocessor. Preprocessor is part of the compiler that processes
a preprocessor directives before the main compilation takes place
• They are not regular code lines with expressions but indications for
the compiler's preprocessor. In this case the directive #include
<iostream> tells the preprocessor to include the iostream standard
file. This specific file (iostream) includes the declarations of the
basic standard input-output library in C++.
Structure of a program
using namespace std;
All the elements of the standard C++ library are declared within what
is called a namespace, the namespace with the name std. So in order
to access its functionality we declare with this expression that we will
be using these entities. This line is very frequent in C++ programs
that use the standard library, and in fact it will be included in most of
the source codes.
Structure of a program
int main( )
This line corresponds to the beginning of the definition of the main function.
The main function is the point by where all C++ programs start their execution,
independently of its location within the source code. It does not matter whether
there are other functions with other names defined before or after it - the
instructions contained within this function's definition will always be the first
ones to be executed in any C++ program. For that same reason, it is essential
that all C++ programs have a main function.
Structure of a program
• The word main is followed in the code by a pair of parentheses
(()). That is because it is a function declaration: In C++, what
differentiates a function declaration from other types of
expressions are these parentheses that follow its name.
Optionally, these parentheses may enclose a list of parameters
within them.
• Right after these parentheses we can find the body of the main
function enclosed in braces ({}). What is contained within these
braces is what the function does when it is executed.
Structure of a program
Cout << “Hello World”;
• This line is a C++ statement. A statement is a simple or
compound expression that can actually produce some effect.
In fact, this statement performs the only action that
generates a visible effect in our first program. cout
represents the standard output stream in C++, and the
meaning of the entire statement is to insert a sequence of
characters (in this case the Hello World sequence of
characters) into the standard output stream (which usually is
the screen).
Structure of a program
• cout is declared in the iostream standard file within the std
namespace, so that's why we needed to include that specific
file and to declare that we were going to use this specific
namespace earlier in our code.
• Notice that the statement ends with a semicolon character (;).
This character is used to mark the end of the statement and in
fact it must be included at the end of all expression statements
in all C++ programs (one of the most common syntax errors is
indeed to forget to include some semicolon after a statement).
Structure of a program
return 0;
• The return statement causes the main function to finish. return
may be followed by a return code (in our example is followed
by the return code 0). A return code of 0 for the main function
is generally interpreted as the program worked as expected
without any errors during its execution. This is the most usual
way to end a C++ console program.

You might also like