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

C++ 2

Uploaded by

mhammadnjmaden45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views24 pages

C++ 2

Uploaded by

mhammadnjmaden45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

Erbil Polytechnic University

Khabat Technical Institute


Information Technology Department
First Stage

Report About:-

C++

Prepared by : Abdulkhaliq Jalal Latif

Prepared for : Mr. Azad Goran

1
Introduction to C++

C++, as we all know is an extension to C language


and was developed by Bjarne stroustrup at bell
labs. C++ is an intermediate level language, as it
comprises a con rmation of both high level and
low level language features. C++ is a statically
typed, free form, multi-paradigm, compiled
general-purpose language.
C++ is an Object Oriented Programming
language but is not purely Object Oriented. Its
features like Friend and Virtual, violate some of
the very important OOPS features, rendering this
language unworthy of being called completely
Object Oriented. Its a middle level language.

2
fi
Bene ts of C++ over C
Language

The major di erence being OOPS concept, C++ is


an object oriented language whereas C is a
procedural language. Apart form this there are
many other features of C++ which gives this
language an upper hand on C language.
Following features of C++ makes it a stronger
language than C,
1. There is Stronger Type Checking in C++.
2. All the OOPS features in C++ like Abstraction,
Encapsulation, Inheritance etc makes it more
worthy and useful for programmers.
3. C++ supports and allows user de ned
operators (i.e Operator Overloading) and
function overloading is also supported in it.
4. Exception Handling is there in C++.
5. The Concept of Virtual functions and also
Constructors and Destructors for Objects.
6. Inline Functions in C++ instead of Macros in C
language. Inline functions make complete
function body act like Macro, safely.
7. Variables can be declared anywhere in the
program in C++, but must be declared before
they are used.

3
fi
ff
fi
Object Oriented
Programming in C++
Object Oriented programming is a programming
style that is associated with the concept of Class,
Objects and various other concepts revolving
around these two, like Inheritance, Polymorphism,
Abstraction, Encapsulation etc.

4
Class
Here we can take Human Being as a class. A
class is a blueprint for any functional entity which
de nes its properties and its functions. Like
Human Being, having body parts, and performing
various actions.

Inheritance

Considering HumanBeing a class, which has


properties like hands, legs, eyes etc, and
functions like walk, talk, eat, see etc. Male and
Female are also classes, but most of the
properties and functions are included in
HumanBeing, hence they can inherit everything
from class HumanBeing using the concept of
Inheritance.

5
fi
Objects

My name is Abhishek, and I am an instance/


object of class Male. When we say, Human Being,
Male or Female, we just mean a kind, you, your
friend, me we are the forms of these classes. We
have a physical existence while a class is just a
logical de nition. We are the objects.

Abstraction

Abstraction means, showcasing only the required


things to the outside world while hiding the
details. Continuing our example, Human Being's
can talk, walk, hearing, eat, but the details are
hidden from the outside world. We can take our
skin as the Abstraction factor in our case, hiding
the inside mechanism.

Encapsulation
This concept is a little tricky to explain with our
example. Our Legs are binded to help us walk.
Our hands, help us hold things. This binding of the
properties to functions is called Encapsulation.

6
fi
Polymorphism

Polymorphism is a concept, which allows us to


rede ne the way something works, by either
changing how it is done or by changing the parts
using which it is done. Both the ways have
di erent terms for them.
If we walk using our hands, and not legs, here we
will change the parts used to perform something.
Hence this is called Overloading.
And if there is a de ned way of walking, but I wish
to walk di erently, but using my legs, like
everyone else. Then I can walk like I want, this will
be called as Overriding.

OOPS Concept De nitions

Now, let us discuss some of the main features of


Object Oriented Programming which you will be
using in C++(technically).

1. Objects
2. Classes
3. Abstraction
4. Encapsulation
5. Inheritance

7
ff
fi
ff
fi
fi
6. Overloading
7. Exception Handling

Objects

Objects are the basic unit of OOP. They are


instances of class, which have data members and
uses various member functions to perform tasks.

Class

It is similar to structures in C language. Class can


also be de ned as user de ned data type but it
also contains functions in it. So, class is basically
a blueprint for object. It declare & de nes what
data variables the object will have and what
operations can be performed on the class's
object.

8
fi
fi
fi
Abstraction

Abstraction refers to showing only the essential


features of the application and hiding the details.
In C++, classes can provide methods to the
outside world to access & use the data variables,
keeping the variables hidden from direct access,
or classes can even declare everything accessible
to everyone, or maybe just to the classes
inheriting it. This can be done using access
speci ers.

Encapsulation

It can also be said data binding. Encapsulation is


all about binding the data variables and functions
together in class.

9
fi
Inheritance

Inheritance is a way to reuse once written code


again and again. The class which is inherited is
called the Base class & the class which inherits is
called the Derived class. They are also called
parent and child class.
So when, a derived class inherits a base class, the
derived class can use all the functions which are
de ned in base class, hence making code
reusable.

Polymorphism

It is a feature, which lets us create functions with


same name but di erent arguments, which will
perform di erent actions. That means, functions
with same name, but functioning in di erent ways.
Or, it also allows us to rede ne a function to
provide it with a completely new de nition. You
will learn how to do this in details soon in coming
lessons.

10
fi
ff
ff
fi
ff
fi
Exception Handling

Exception handling is a feature of OOP, to handle


unresolved exceptions or errors produced at
runtime.

Basic Concepts of C++


In this section we will cover the basics of C++, it
will include the syntax, Variables, operators, loop
types, pointers, references and information about
other requirements of a C++ program. You will
come across lot of terms that you have already
studied in C.

Syntax and Structure of C++


program

Here we will discuss one simple and basic C++


program to print "Hello this is C++" and its
structure in parts with details and uses.

11
First C++ program

#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello this is C++";
}

Header les are included at the beginning just


like in C program. Here iostream is a header le
which provides us with input & output streams.
Header les contained predeclared function
libraries, which can be used by users for their
ease.

Using namespace std, tells the compiler to use


standard namespace. Namespace collects
identi ers used for class, object and variables.
NameSpace can be used by two ways in a
program, either by the use of using statement at
the beginning, like we did in above mentioned
program or by using name of namespace as pre x
before the identi er with scope resolution (::)
operator.

Example: std::cout << "A";

12
fi
fi
fi
fi
fi
fi
main(), is the function which holds the executing
part of program its return type is int.
cout <<, is used to print anything on screen,
same as printf in C language. cin and cout are
same as scanf and printf, only di erence is that
you do not need to mention format speci ers like,
%d for int etc, in cout & cin.

Comments in C++ Program

For single line comments, use // before


mentioning comment, like

cout<<"single line"; // This is single line


comment

For multiple line comment, enclose the comment


between /* and */

/*this is
a multiple line
comment */

13
ff
fi
Creating Classes in C++
Classes name must start with capital letter, and
they contain data Variables and member
functions. This is a mere introduction to classes,
we will discuss classes in detail throughout the C+
+ tutorial.

class Abc
{
int i; //data variable
void display() //Member Function
{
cout << "Inside Member Function";
}
}; // Class ends here

int main()
{
Abc obj; // Creatig Abc class's object
obj.display(); //Calling member function
using class object
}

This is how a class is de ned, once a class is


de ned, then its object is created and the member
functions are used.
Variables can be declared anywhere in the entire
program, but must be declared, before they are
used. Hence, we don't need to declare variable at
the start of the program.

14
fi
fi
Datatypes and
Modi ers in C++
Let's start with Datatypes. They are used to de ne
type of variables and contents used. Data types
de ne the way you use storage in the programs
you write. Data types can be of two types:
1. Built-in Datatypes
2. User-de ned or Abstract Datatypes

Built-in Data Types

These are the datatypes which are prede ned and


are wired directly into the compiler. For eg: int,
char etc.

15
fi
fi
fi
fi
fi
User de ned or Abstract data
types
These are the type, that user creates as a class or
a structure. In C++ these are classes where as in
C language user-de ned datatypes were
implemented as structures.

Enum as Datatype in C++

Enumerated type declares a new type-name along


with a sequence of values containing identi ers
which has values starting from 0 and incrementing
by 1 every time.
For Example:

enum day(mon, tues, wed, thurs, fri) d;

Here an enumeration of days is de ned which is


represented by the variable d. mon will hold value
0, tue will have 1 and so on. We can also explicitly
assign values, like, enum day(mon, tue=7,
wed);. Here, mon will be 0, tue will be assigned 7,
so wed will get value 8.

16
fi
fi
fi
fi
Modi ers in C++
In C++, special words(called modi ers) can be
used to modify the meaning of the prede ned
built-in data types and expand them to a much
larger set. There are four datatype modi ers in C+
+, they are:
1. long
2. short
3. signed
4. unsigned
The above mentioned modi ers can be used
along with built in datatypes to make them more
precise and even expand their range.
Below mentioned are some important points you
must know about the modi ers,
• long and short modify the maximum and
minimum values that a data type will hold.
• A plain int must have a minimum size of short.
• Size hierarchy : short int < int < long int
• Size hierarchy for oating point numbers is :
float < double < long double
• long oat is not a legal type and there are no
short oating point numbers.
• Signed types includes both positive and
negative numbers and is the default type.

17
fl
fl
fi
fl
fi
fi
fi
fi
fi
• Unsigned, numbers are always without any
sign, that is always positive.

Functions in C++
Functions are used to provide modularity to a
program. Creating an application using function
makes it easier to understand, edit, check errors
etc.

Basic Syntax for using


Functions in C++

Here is how you de ne a function in C++,

return-type function-name(parameter1,
parameter2, ...)
{
// function-body
}

18
fi
• return-type: suggests what the function will
return. It can be int, char, some pointer or
even a class object. There can be functions
which does not return anything, they are
mentioned with void.
• Function Name: is the name of the function,
using the function name it is called.
• Parameters: are variables to hold values of
arguments passed while function is called. A
function may or may not contain parameter
list.

// function for adding two values


void sum(int x, int y)
{
int z;
z = x + y;
cout << z;
}

int main()
{
int a = 10;
int b = 20;
// calling the function with name 'sum'
sum (a, b);
}

Here, a and b are two variables which are sent as


arguments to the function sum, and x and y are
parameters which will hold values of a and b to
perform the required operation inside the function.
19
Function body: is the part where the code
statements are written.

Declaring, De ning and


Calling a Function

Function declaration, is done to tell the compiler


about the existence of the function. Function's
return type, its name & parameter list is
mentioned. Function body is written in its
de nition. Let’s understand this with help of an
example.

#include < iostream>


using namespace std;

//declaring the function


int sum (int x, int y);

int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling the
function
cout << c;
}

//defining the function


int sum (int x, int y)

20
fi
fi
{
return (x + y);
}

Here, initially the function is declared, without


body. Then inside main() function it is called, as
the function returns sumation of two values, and
variable c is there to store the result. Then, at last,
function is de ned, where the body of function is
speci ed. We can also, declare & de ne the
function together, but then it should be done
before it is called.

Calling a Function

Functions are called by their names. If the function


is without argument, it can be called directly using
its name. But for functions with arguments, we
have two ways to call them,
1. Call by Value
2. Call by Reference

21
fi
fi
fi
Call by Value
In this calling technique we pass the values of
arguments which are stored or copied into the
formal parameters of functions. Hence, the
original values are unchanged only the parameters
inside function changes.

void calc(int x);

int main()
{
int x = 10;
calc(x);
printf("%d", x);
}

void calc(int x)
{
x = x + 10 ;
}

Output : 10

In this case the actual variable x is not changed,


because we pass argument by value, hence a
copy of x is passed, which is changed, and that
copied value is destroyed as the function
ends(goes out of scope). So the variable x inside
main() still has a value 10.

22
But we can change this program to modify the
original x, by making the function calc() return a
value, and storing that value in x.

int calc(int x);

int main()
{
int x = 10;
x = calc(x);
printf("%d", x);
}

int calc(int x)
{
x = x + 10 ;
return x;
}

Output : 20

Call by Reference

In this we pass the address of the variable as


arguments. In this case the formal parameter can
be taken as a reference or a pointer, in both the
case they will change the values of the original
variable.

23
void calc(int *p);

int main()
{
int x = 10;
calc(&x); // passing address of x as
argument
printf("%d", x);
}

void calc(int *p)


{
*p = *p + 10;
}

Output : 20

NOTE: If you do not have a prior knowledge of


pointers, do study pointers rst.

24
fi

You might also like