You are on page 1of 9

1. Introduction to C++ Mbuke, Juliana L.

Topics covered

•Introduction to OOP

•Variables and assignments •Data types

•Keywords, identifiers, statements •Classes and objects

Introduction

•Programming languages are of two categories namely structural programming languages such
as C , PASCAL, FORTRAN, COBOL etc. and object oriented programming languages such as
C++, C#, JAVA, Python, VisualBasic.NET etc

•C++ is an Object-Oriented Programming (OOP) language and is considered to be an extension


of C. All concepts of C are applicable to C++ as well. •Unlike C, C++ has facilities to do OOP
and the concept behind object oriented languages is to combine both data and functions together.

Concepts of OOP

•Classes

–A class is an enhanced structure that provides object-oriented features of C++, it is defined by


describing the set of data it can represent and the set of functions that can operate on that data. –
Classes encapsulate data through the use of member data and member function

•Objects

–An object is an instance of a class. An object may be uniquely defined by a specific name.

–Memory is allocated to objects and an object can contain attributes.

•Inheritance

–It is the process by which one class can derive features of another class. The existing class is
called the base class, and new class is called derived class.

–The derived class not only executes common features from base class but also executes its own
features
–Inheritance is used to reduce source code of OOP. Without use of inheritance, each class has to
define all its characteristics explicitly.

•Reusability

–Once a class is created and debugged, it can be distributed to other programmers for use in their
programs. –Programmers can take an existing class and without modifying it, add additional
features and capability for use.

•Data encapsulation

–Encapsulation is the process of combining member functions and data, it manipulate and keep
them safe from outside interferences. In OOP an object is created by including data and functions
for input and output manipulation and thus supports encapsulation –Encapsulated data is not
accessible to the outside world (other classes) and only those functions which are inside the class
can access it. •The insulation of data from direct access by a program is called as data hiding

•Data abstraction

Data abstraction refers to, providing only essential information to the outside world and hiding
their background details, i.e., to represent the needed information in program without presenting
the details.

Data abstraction is a programming (and design) technique that relies on the separation of
interface and implementation.

•Object oriented programs can be easily upgraded at anytime •Using inheritance, redundant
program codes can be eliminated and the use of previously defined classes may be continued

•The encapsulation feature provided by OOP languages allows programmer to define the class
with many functions and characteristics and only few functions are exposed to the user •All
OOP languages can create an extended and reusable parts of programs

Variables and assignments

Variables

•C++ and most other programming languages use programming constructs known as variables
to name and store data.
•The name of a variable is called an identifier. It must start with either a letter or an underscore
symbol •All variables must be declared before they are used •The syntax for variable declaration
is as follows: –Type variable identifier;

– For instance: int count;

double length;

Assignments

•In an assignment statement, the expression on the right hand side of the equal sign is evaluated
and then the variable on the left hand side of the equal sign is set equal to this value

•The syntax for assignment is as follows: – Variable = Value or expression;

– For instance: unit_cost = 20;

TotalCost = unit_cost * quantity;

Data types

Data types

•C++ supports a variety data types each of which may be represented differently within
computers memory. This allows programmer to select the appropriate type to the needs of the
application

•Data types in C++ can be broadly classified as:

1.Primary data types

2.User-defined data types

Primary data types

•All C++ compilers support four primary data types namely: –Integer data type represented as int
•These are whole numbers which are used frequently, occupy two bytes for their storage. Signed
and unsigned int data type are used. –Floating-point data type represented as float •These are
real numbers generally requires four bytes of storage space in memory –Double-precision
floating-point data type represented as double •This type of floating point requires eight bytes of
memory storage –Character data type represented as char •This data type generally requires one
byte of memory for their storage. Signed and unsigned char data type are used.
User-defined data types •C++ supports a feature known as type definition that allows user to
define new data types that are equivalent to an existing data types.

–Type definition data type

–Enumerated data type

User-defined data types Type definition data type •The keyword typedef is used to define new
data type names.

•The syntax for defining a new data type is: –Typedef data type identifier; where identifier
refers to the new names given to the data type.

•Some of examples are:

–Typedef int num;

–Typedef char sname;

Where num symbolize int and sname symbolize char. They can later be used to declare variables
such as;

num unit1, unit2; sname name[5]; Where unit1 and unit2 are declared as integer variables, and
name is declared are character array variable.

User-defined data types Enumerated data type

•The enumerated data type gives an opportunity to invent your own data type with a set of named
integer constants represented by identifiers that specifies all the legal values a variable of that
type may have. •The syntax for enumerated data type is: –enum identifier {enumeration list};

Where enum is a keyword used to specify the enumerated data type, identifier is a user-defined
enumerated data type which can be used to declare the enumeration list.

User-defined data types Enumerated data type


For instance:

enum day {monday, tuesday, wednesday, thursday, friday}; •This creates a new type day in
which the enumeration list (i.e. Monday, Tuesday....) are set automatically to integers 0 to 5.

•To number the enumeration list from 1 to 5, enum declaration should be:

•enum day {Monday = 1, tuesday, wednesday, thursday, frida friday};


y}; , where the first enumeration
list is explicitly set to 1 and hence remaining values are incremented from 1

User-defined
defined data types Enumerated data type

Keywords, identifiers, statements

Keywords

•C++ keywords are reserved words by the compiler and have fixed meanings.All C keywords are
valid in C++.

•The common keywords in C++ include auto break case char const continue tinue default do double
else enum extern float for goto if int long register return short signed sizeof static struct switch
typedef union unsigned void volatile while •Additional keywords include asm catch class delete
friend inline new operator private protected public template this throw try virtual

Identifiers
•Identifiers are names of variables, functions, arrays. They are user-defined names consisting of
a sequence of letters and digits with a letter as a first character.

•The ( _ ) underscore symbol can be used as an identifier though in general underscores are used
to link two words in long identifiers.

•The identifier name should not be a C++ keyword.

Statements

•Comment statement

–Double slashes (//) or (/*.....*/) are used to represent a Comment statement. It is not an
executable statement and it is used for documentation purpose. •Input and output statements –
C++ provides C in as an alternative to Scanf () function for reading input from the keyboard. C
in is an object predefined in C++ standard library. The syntax is as follows:

•Cin >> variable;

–C++ provides C out as an alternative to Printf () function for displaying the output on the
screen. C out is an object predefined in C++ standard library. The syntax is as follows:

•Cout << variable;

•Review selection statements (If, if...else, switch) and iteration statements (while, do...while,
for). Practice with C++ programming.

Classes and objects

Objects and classes

•The fundamental idea behind object-oriented languages is to combine both data and functions
into a single unit that operates on the data, such a unit is called as an object.

•A class is a new way of creating and implementing a user-defined data type. It is an extension of
structure in C. •The syntax for declaring a class is as follows:

Class declaration

Class <name of the class>

Private:
Data members;

Member functions;

Protected:

Data members;

Member functions;

Public:

Data members;

Member functions;

};

Class declaration

•A class is declared with a name preceded by the keyword class, body of the class is enclosed by
{ }; •Variables declared inside a class are said to be members of that class. Functions that are
declared as said to be part of a class called member functions.

•A class member can be specified with access specifiers private, protected or public.

–By default all members declared inside a class are private to that class. Private members can not
be accessed from outside the class. –To access members from outside the class, members should
be declared a public. All functions and variables declared as public are accessible by other
members of the class and by any other part of the program that contains the class.

The scope resolution operator •When defining a member function outside the class, specify class
name followed by function name separated by an operator, a double colon symbol ( :: ), this is
called the scope resolution operator.

•The syntax for defining a member function using the scope resolution operator is as follows: –
Return type class name :: function Name (list of arguments)

A simple C++ program to calculate addition of two integers


A C++ program to calcul
calculate
ate addition of two integer using class declaration

Lab practice
•Write the following programs using C++ programming language

–A program to change temperature back and forth between Centigrade and Fahrenheit .

–A program to display module grades depending on the number of input marks.

–A program to calculate areas of rectangle, triangle and square using class declarations.

You might also like