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

C++ 2

The document provides an introduction to C++, covering its history, benefits over C, object oriented programming concepts in C++ including class, objects, inheritance, polymorphism, abstraction and encapsulation. It also discusses basic C++ concepts including syntax, variables, operators, loops, pointers, references and exception handling.

Uploaded by

mhammadnjmaden45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views25 pages

C++ 2

The document provides an introduction to C++, covering its history, benefits over C, object oriented programming concepts in C++ including class, objects, inheritance, polymorphism, abstraction and encapsulation. It also discusses basic C++ concepts including syntax, variables, operators, loops, pointers, references and exception handling.

Uploaded by

mhammadnjmaden45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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 confirmation 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
Benefits of C++ over
C Language

The major difference 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 defined
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
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
defines 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
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 definition. 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
Polymorphism

Polymorphism is a concept, which allows us to


redefine 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
different 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 defined way of walking, but I wish
to walk differently, but using my legs, like
everyone else. Then I can walk like I want, this will
be called as Overriding.

OOPS Concept Definitions

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
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 defined as user defined data type but it
also contains functions in it. So, class is basically
a blueprint for object. It declare & defines what
data variables the object will have and what
operations can be performed on the class's
object.

8
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
specifiers.

Encapsulation

It can also be said data binding. Encapsulation is


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

9
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
defined in base class, hence making code
reusable.

Polymorphism

It is a feature, which lets us create functions with


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

10
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 files are included at the beginning just


like in C program. Here iostream is a header file
which provides us with input & output streams.
Header files 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
identifiers 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 prefix
before the identifier with scope resolution (::)
operator.

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

12
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 difference is
that you do not need to mention format specifiers
like,
%d for int etc, in cout & cin.

Comments in C++ Program

For single line comments, use // before


mentioning comment, like

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

For multiple line comment, enclose the comment


between /* and */

/*this is
a multiple line
comment */

13
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 defined, once a class is


defined, 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
Datatypes and
Modifiers in C++
Let's start with Datatypes. They are used to define
type of variables and contents used. Data types
define the way you use storage in the programs
you write. Data types can be of two types:
1. Built-in Datatypes
2. User-defined or Abstract Datatypes

Built-in Data Types

These are the datatypes which are predefined and


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

15
User defined 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-defined datatypes were
implemented as structures.

Enum as Datatype in C++

Enumerated type declares a new type-name along


with a sequence of values containing identifiers
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 defined 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
Modifiers in C++
In C++, special words(called modifiers) can be
used to modify the meaning of the predefined
built-in data types and expand them to a much
larger set. There are four datatype modifiers in C+
+, they are:
1. long
2. short
3. signed
4. unsigned
The above mentioned modifiers 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 modifiers,
• 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 floating point numbers is :
float < double < long double
• long float is not a legal type and there are no
short floating point numbers.
• Signed types includes both positive and
negative numbers and is the default type.

17
• 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 define a function in C++,

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

18
• 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
}

int main()
{
int a =
10; int b
// 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, Defining
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
definition. 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
int c = sum (a, b); //calling the
function
cout << c;
}

20
//defining the function
int sum (int x, int y)

21
{
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 defined, where the body of function is
specified. We can also, declare & define 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

22
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.

23
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.

24
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 first.

25

You might also like