Sie sind auf Seite 1von 87

About the Basic task of programming

About procedural programming

About object-oriented programming

About the C++ programming environment

How to create a main() function

How to work with variables and the const qualifier

How to create comments

How to use libraries and preprocessor directives

How to use cout and cin

How to work with classes

Programming a computer involves writing instructions


that enable a computer to carry out a single task or a
group of tasks
A computer programming language requires learning
both vocabulary and syntax
Programmers use many different programming
languages, including BASIC, Pascal, COBOL, RPG,
and C++
The rules of any language make up its syntax
Machine language is the language that computers can
understand; it consists of 1s and 0s

A translator (called either a compiler or an interpreter)


checks your program for syntax errors
A logical error occurs when you use a statement that,
although syntactically correct, doesnt do what you
intended
You run a program by issuing a command to execute the
program statements
You test a program by using sample data to determine
whether the program results are correct

All programming languages provide methods for directing


output to a desired object, such as a monitor screen,
printer or file

Similarly, all programming languages provide methods for


sending input into the computer program so that it can be
manipulated

In addition, all programming languages provide for


naming locations in computer memory

These locations commonly are called variables (or


attributes)

A programming paradigm is a fundamental style of


computer programming.
There are four main paradigms:
object-oriented
Imperative
functional
logic programming
Rule-oriented
Procedure Oriented

Functional Programming

Scheme, ML
Functions are first-class objects

Logic Programming
Prolog
Declarative Programming
Imperative Programming
C, Java, Pascal
Focuses on change of state

Rule Oriented Programming


Lisp, OBJ*
Procedure Oriented
Ada, Basic, Cobol, C etc
Object Oriented
C++, Java, Objective C, Python etc

Main Program
Global Data

Function1

Function2

Function3

Local Data

Local Data

Local Data

Advantages
Its relative simplicity, and ease of implementation of compilers
and interpreters
The ability to re-use the same code at different places in the
program without copying it.
An easier way to keep track of program flow.
The ability to be strongly modular or structured.

Disadvantages
Needs only less memory.
Data is exposed to whole program, so no security for
data.
Difficult to relate with real world objects.
Difficult to create new data types reduces
extensibility.
Importance is given to the operation on data rather
than the data.

To solve a large problem, break the problem into


several pieces and work on each piece separately.
To solve each piece, treat it as a new problem that can
itself be broken down into smaller problems.
Repeat the process with each new piece until each can
be solved directly, without further decomposition.

Main Program
Global Data

Function1
Local
Data

Module 1

Function2

Function3

Function4

Function5

Local Data

Local Data

Local Data

Local Data

Module 2

Module 3

Disadvantages
Repletion of code in several places within a single program.
This long code in turn makes it difficult to effectively locate
and fix errors in the program.
lacks information hiding.
Clash of variables when different parts of the program
overwrite the same variable.

Classes
Objects
Data Abstraction
Encapsulation
Inheritance
Polymorphism

Message passing
Extensibility
Persistence
Constructors
Delegation
Generality
Multiple Inheritance

Object is a self-contained component that contains properties


and methods needed to make a certain type of data useful.
Objects have the data fields(attributes that describe the object)
and associated procedures known as methods.
Objects are instances of classes and used to interact with one
another to design applications and computer programs

Object-Oriented Programming
An object is anything that can be represented by data in
a computers memory and manipulated by a computer
program.

Numbers

Object-Oriented Programming
An object is anything that can be represented by data in
a computers memory and manipulated by a computer
program.

Text

Object-Oriented Programming
An object is anything that can be represented by data in
a computers memory and manipulated by a computer
program.

Pictures

Object-Oriented Programming
An object is anything that can be represented by data in
a computers memory and manipulated by a computer
program.

Sound

Object-Oriented Programming
An object is anything that can be represented by data in
a computers memory and manipulated by a computer
program.

Video

Object-Oriented Programming
An object is anything that can be represented by data.

Object-Oriented Programming
To a computer, an object is
simply something that can be
represented by data in the

computers memory and


manipulated by computer

programs.

Object-Oriented Programming
The data that represent the
object are organized into a set
of properties.
The values stored in an
objects properties at any one
time form the state
of an object.

Name:

PA 3794

Owner:

US Airlines

Location: 39 52 06 N 75 13 52 W
Heading:

271

Altitude:

19 m

AirSpeed: 0
Make:

Boeing

Model:

737

Weight:

32,820 kg

How do programmers get by implementing abstraction?


They use a programming structure called a class.
A class presents a blueprint of an object, its properties
and its methods.
Class vehicle
{ private:

public:

char name[20];
int age;
char sex;
listen();
speak();
talk();

Attributes
Message
Passing
Member
Functions

Data Hiding

Programmers store an objects data in attributes, also


called properties.
Attributes provide us a way to describe an object,
similar to adjectives in grammar.
We can read property values or change properties,
assigning values.

Whereas attributes describe an object, methods allow


us to access object data. Methods are like verbs in
grammar.
We can manipulate object data, stored in attributes,
using methods.

Object-oriented programming is inherently tied to user


interaction. Programs record interaction in the form of
events.
Events are changes in an objects environment to which
it can react.

One of the chief advantages of object-oriented


programming is the idea that programmers can
essentially focus on the big picture and ignore
specific details regarding the inner-workings of an
object. This concept is called abstraction.

To create an object based on a class, we create an


instance of that class. This process is called
instantiation.
In C++, Java, JavaScript and other languages, we use a
special method called a constructor method to create an
instance of an object.

Abstraction in OOP is closely related to a concept


called encapsulation.
Encapsulation means that a group of related properties,
methods, and other members are treated as a single unit
or object. Objects can control how properties are
changed and methods are executed.
Data and the ways to get at that data are wrapped in a
single package, a class. The only way to access such
data is through that package. This idea translates to
information hiding.

Inheritance describes the ability to create new classes


based on an existing class. The new class inherits all
the properties and methods and events of the base class,
and can be customized with additional properties and
methods.

Forms of Inheritance
A

B
(a) Single Inheritance
A

B
C

(b) Multiple Inheritance

(c) Hierarchical Inheritance

(a) Multi-Level Inheritance (b) Hybrid Inheritance

D
(b) Multipath Inheritance
41

Polymorphism allows single name/operator to be


associated with different operations depending on the
type of data passed to it.
Function Overloading
Operator Overloading
Dynamic Binding (Virtual functions)

Allows creating several methods with the same name


which differ from each other in the type of the input
and the output of the function.
int add_num(int, int, int);
int add_num(float, float);
float add_num(int, float);
int add_num( float, int, float);

Operators can be extended to work not just with built-in types


but also classes. A programmer can provide his or her own
operator to a class by overloading the built-in operator to
perform some specific computation.
Complex a(1.2,1.3);
Complex b(2.1,3);
Complex c = a+b;

The decision is made at run-time based upon the type of the


actual object.

It is a process of invoking an operation on an object.


Then in response the corresponding method gets
executed.

Extension to functionality of the existing software.


Achieved by ---- Inheritance & Abstract classes

C++ is an object oriented programming language.


C++ is an extension of C with a major addition of the
class construct feature.
C++ is superset of C.
Features like classes, inheritance, function overloading,
and operator overloading make C++ a truly objectoriented language.
OO features in C++ allow programmers to build large
programs with clarity, extensibility and ease of
maintenance, incorporating the spirit and efficiency of
C.

/* Simple hello world program*/


# include <iostream>// This is include directive
using namespace std;
int main()
{
cout << Hello World!; //C++ statement
return 0;
}

Keywords
Identifiers
Constants
Strings
Operators

Procedural language.
Data is not secured.
Low level language
Top down approach
Function driven
No Function overloading
No Namespace feature

Procedural and Object


oriented.
Data is secured.
Middle level language
Bottom up approach
Object driven
Has Function overloading
Namespace feature exists

C++

Printf and Scanf


No Exception Handling
No reference variables
Structures has Member
variables only
Malloc and calloc

Cout and cin


Exception handling exists
Reference variables exists
Structures has Member
variables and Member
functions.
Bool data type
new and delete

C++

Keywords
Identifiers
Constants
Strings
Operators

Some words are reserved for implementing the specific C++


language features.
asm, auto, bool, break, case, catch, char,
class,
const,
const_cast,
continue,
default, delete, do, double, dynamic_cast,
else,
enum,
explicit,
export,
extern,
false,
float,
for,
friend,
goto,
if,
inline, int, long, mutable, namespace, new,
operator,
private,
protected,
public,
register, reinterpret_cast, return, short,
signed,
sizeof,
static,
static_cast,
struct, switch, template, this, throw,
true,
try,
typedef,
typeid,
typename,
union, unsigned, using, virtual, void,
volatile, wchar_t, while

Keywords
Identifiers
Constants
Strings
Operators

Identifiers refer to the names of variables, functions,


arrays, classes , etc. created by the programmer.

Rules for creating it.

Only alphabet characters, digits and underscore are permitted.


The name cannot start with digit.
Uppercase and lowercase letters are distinct.
A declared keyword cannot be used as a variable.

C++ Data
Types

User-defined type

Built-in
type

structure
union
class
enumeration

Integral
type

int

Derived type
array
function
pointer
reference

Floating
type

Void

char

float

double

Type

Bytes

Range

char

-128 to 127
signed: -128 to 127
unsigned: 0 to 255

short int

-31768 to 32767
signed: -32768 to 32767
unsigned: 0 to 65535

int

-32768 to 32767
signed: -31768 to 32767
unsigned: 0 to 65535

long int

-2147483648 to 2147483647
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295

float

3.4E-38 to 3.4E+38

double

1.7E-308 to 1.7E+308

long double

10

3.4E-4932 to 1.1E+4932

Structures and Classes


Basis for OOP.
Classes enable to combine data and procedures.

Enumerated Data Type

It provides a way to attaching names to the numbers


Increase comprehensibility of the code.
Alternative mean for creating symbolic constants
Enumerates a list of words by assigning them values 0,1,2 and
so on.
enum shape {circle, square, triangle};
enum colour {red, blue=4, green=8};

Arrays
Values of similar type stored in continuous memory locations.
int a[10]; char string[3]=xyz;

Functions
Set of statements to perform specific tasks

Pointers
Special variables to store the memory location of other
variables.
Used in referencing memory.
Concept of constant pointer introduced in c++.

Keywords
Identifiers
Constants
Strings
Operators

Refer to fixed values that do not change in thw


execution of the program.

123
12.34
037
0x2
C++
A
Lab

//decimal integer
//floating point integer
//octal integer
//Hexa decimal
//string constant
//character constant
//wide-character constant

Using the qualifier const


const float pi = 3.14;

Using enum
enum{x,y,z};
enum{x=200,y=300,z=400};
enum{off,on};

Keywords
Identifiers
Constants
Strings
Operators

Variables that can store non-numerical values that are longer


than one single character are known as strings.
The C++ language library provides support for strings through
the standard string class.
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring;
return 0;
}

Keywords
Identifiers
Constants
Strings
Operators

Constant expressions
Integral expressions
Float expressions
Pointer expressions
Relational expressions
Logical expressions
Bitwise expressions

The if statement
Simple if statement
if(expression)
{
statement 1;
}
statement 2;
statement 3;

if..else statement
if(expression)
{
statement 1;
}
else
{
statement 2;
}
statement 3;

The switch statement


switch(expression)
{
case 1:
statement1;
break|continue;
case 2:
statement2;
continue|break;
.
.
.
default:
statement3;
}
statement4;

The do-while statement

do
{
statement1;
}
while(condition);
statement2;

The while statement

while(condition)
{
statement1;
}
statement2;

The for statement

for (intialization;condition;increment)
{
statement1;
}
statement2;

A piece of code that perform specific task.


Introduces modularity in the code.
Reduces the size of program.
C++ has added many new features to the functions to
make them more reliable and flexible.
It can be overloaded.

Function declaration
return-type function-name (argument-list);
void show();
float volume(int x,float y,float z);

Function definition
return-type function-name(argument-list)
{
statement1;
statement2;
}

Function call
function-name(argument-list);
volume(a,b,c);

The main function


Returns a value of type int to the operating system.

int main()
{

return(0);
}

Parameter Passing
Pass by value
Pass by reference
Reference variable

Pointers

void swap (int &a, int &b)


{
int t=a;
a=b;
b=t;
}

void swap(int *a, int *b)


{
int t;
t=*a;
*a=*b;
*b=t;
}

swap(m,n);

swap(&m,&n);

Parameter passing
Return by reference

int & max (int &x, int &x)


{
if(x>y)
return x;
else
return y;
}
max(a,b)=-1;

C++ allows to use the same function name to create


functions that perform a variety of different tasks.
This is know as function polymorphism in OOP.
int add (int a, int b); //prototype 1
int add (int a, int b, int c); //prototype 2
double add (double x, double y);//prototype 3
double add (int p, double q);//prototype 4
double add (double p, int q); //prototype 5

add(5,10);//uses prototype 1
add(15,10.0);//uses prototype 4
add(12.5,7.5); //uses prototype 3
add(5,10,15); //uses prototype 2
add(0.75,5); //uses prototype5

Structures Revisited

Makes convenient to handle a group of logically related data items.


struct student //declaration
{
char name[20];
int roll_number;
float total_marks;
};
struct student A;// C declaration
student A;
//C++ declaration
A.roll_number=999;
A.total_marks=595.5;
Final_Total=A.total_marks + 5;

Limitations
C doesnt allow it to be treated like built-in data types.
struct complex{float x; float y;};
struct complex c1,c2,c3;
c3=c1+c2;//Illegal in C

They do not permit data hiding.

Can hold variables and functions as members.


Can also declare some of its members as private.
C++ introduces another user-defined type known as
class to incorporate all these extensions.

Class is a way to bind the data and procedures that operates on


data.

Class declaration:
class class_name
{
private:
variable declarations;//class
function declarations;//members
public:
variable declarations;//class
function declarations;//members
};//Terminates with a semicolon

Class members that have been declared as private can


be accessed only from within the class.

Public class members can be accessed from outside the


class also.

Supports data-hiding and data encapsulation features


of OOP.

Objects are run time instance of a class.


Class is a representation of the object, and Object is the
actual run time entity which holds data and function
that has been defined in the class.
Object declaration:
class_name obj1;
class_name obj2,obj3;
class class_name
{}obj1,obj2,obj3;

Accessing class members


Object-name.function-name(actual-arguments);
obj1.setdata(100,34.4);

Defining Member Functions


Outside the class definition.
return-type class-name::function-name (argument declaration)
{
Function body;
}
Inside the class definition.
Same as normal function declaration.

Das könnte Ihnen auch gefallen