You are on page 1of 29

C++ Programming

Preliminaries

Origins of C++
Creation started in 1979 by Bjarne Stroustrup
Initially called C with classes
In 1983 it was called C++
1979 1980s 1990s development and refinement
of C++.
May 1990 C++ was released
1998 ANSI version of C++ was adopted
C++ is the object-oriented version of C.
C++ began as a set of object-oriented extensions to
C
Presently, C++ is twice the size of C, it is one of the
most powerful programming languages ever devised.

What is OOP
OOP is a new way of approaching the job of
programming.
Evolution

Binary machines toggle instructions


Assembly Language for larger programs, uses
symbolic representations of the machine
instructions.
High level languages (fortran, 1st widely used
language) impressive step
1960s gave birth to structured programming

OOP
Structured Programming

Method encouraged by languages such as C and


Pascal
It made programming a lot easy
Size problems (25K 100K SLOC)

Object Oriented Programming

Takes the best ideas of structured programming


and combines them with powerful, new concepts
that encourage you to look at the task of
programming in a new perspective.

OOP
Allows you to easily decompose a
problem into subgroups of related
parts.
These subgroups can be translated into
self-contained units called objects.

Objects
The single most important feature of an
object-oriented language.
An object is a logical entity containing data
and code that manipulates that data.
An object is a variable of a user-defined type.
Within an object some of the code or data
maybe private to the object and inaccessible
by anything outside the object.

Private Object
Object provides a significant level of
protection against accidental
modification or incorrect use.
The linkage of code and data is often
referred to as encapsulation.
In OOP, when you define an object you
are implicitly creating a new data type.

Class
Class is the root of C++.
Before creating objects classes must be
defined first. (to instantiate)
It can contain private (by default) and
public parts
It is suggested to keep all data private.

Polymorphism
It allows one name to be used for several
related but slightly different purposes.
The purpose is to let one name to be used to
specify a general class of action.
Depending upon what type of data it is
dealing with, a specific instance of the
general case is executed.

Inheritance
Is the process by which one object can
acquire the properties of another object.
It supports the concept of classification
With classification, an object need only define
those qualities that make it unique within its
class.
This mechanism makes it possible for one
object to be a specific instance of a more
general case.

Inheritance Example

Visual C++ Environment

Hello World (,)

Hello World
#include<iostream.h>
void main(void)
{
cout<<"Hello World\n";
}

// Reads values for the length and width of a rectangle


// and returns the perimeter and area of the rectangle.
#include <iostream.h>

void main()
{
int length, width;
int perimeter, area;
// declarations
cout << "Length = ";
// prompt user
cin >> length;
// enter length
cout << "Width = ";
// prompt user
cin >> width;
// input width
perimeter = 2*(length+width);
// compute perimeter
area = length*width;
// compute area
cout << endl << "Perimeter is " << perimeter;
cout << endl << "Area is " << area << endl; // output results
}
// end of main program

// Reads values for the length and width of a rectangle


// and returns the perimeter and area of the rectangle.
#include <iostream.h>

comments
I/O

void main(void)
{
int length, width;
int perimeter, area;
// declarations
literal
cout << "Length = ";
// prompt user
cin >> length;
// enter length
; terminate
cout << "Width = ";
// prompt user
cin >> width;
// input width
perimeter = 2*(length+width);
// compute perimeter
= assignment
area = length*width;
// compute area
cout << \nPerimeter is " << perimeter;
cout << endl << "Area is " << area << endl; // output results
endl==\n
}
// end of main program

C++ Statements
output
(left shift)
operator

cout<< I will love C++ <<endl;


new line
identifier
linked
to the
screen
--output

C++ Statements
refers to
input
the
keyboard (right shift)
operator
-- input

Single line
comment

cin >> i;
//variable i is int
/*variable i is
char*/

Multi-line
comment

End for this lecture

You might also like