Vous êtes sur la page 1sur 32

C with C++Object Oriented Programming

Development

By:
Suryakant Kamble

1
What are we doing today?
Introduction of:
C Summary
Objects
Basic Terminology
C++

2
Beginning C Programming
Basic Computer Model
Input CPU Output

Memory

Instructions
+
Data

Von Neumann Architecture


Programming Languages
+1300042774
Machine Languages +1400593419
+1200274027

LOAD A
Assembly Languages ADD B
STORE C

High-Level Languages C=A+B


Creating Programs

C libaray

Edit hello.c compile hello.o Link hello

Source File Object File


Executable
(High-Level (Machine Languages)
Languages)

hello.c hello.obj
Hello, World
1. /* Hello, world program */
2. #include <stdio.h>
3.
4. int main()
5. {
6. printf("hello, ");
7. printf("world\n");
8. return 0;
9. }
Editing: Turbo C++,Borland Compiler.
Compiling: Alt + F9
Executing:Ctrl + F9
Basic Programming Concepts
 Comment To explain what the program is for and why it is
the way it is.
 Preprocessing Done before the program is compiled.
 To include header files containing information about C libraries
 Statement A line of code that tells computer to perform
some action. Always ended with a
semicolon(;).
 Invoking functions
 Function A module often consisting of a set of statements.
 Two functions in the hello world program: main, printf
Escape Sequences
Escape Sequences are used to control printf to do
something other than printing characters.

Modify the hello world program to try out various


escape sequences like:
 \n: Newline. Position cursor at the beginning of the next
line.

 \t: Tab. Move cursor to the next tab stop.

 \a: Alert. Sound the system bell.


Arithmetic Operators
To form expressions + Addition
- Subtraction
Normal order of operations is
* Multiplication
followed. Parentheses can be / Division
used. Modulus
% (remainder)
++ Increment
-- Decrement
Arithmetic Operators: An Example
1. /* Arithmetic operators */
2. #include <stdio.h>
3.
4. int main()
5. {
6. printf("7+3=%d\n",7+3);
7. printf("7-3=%d\n",7-3);
8. printf("7*3=%d\n",7*3);
9. printf("7/3=%d\n",7/3);
10. printf("7%%3=%d\n",7%3);
11. printf("7.0/3.0=%f\n",7.0/3.0);
12. return 0;
13.}
Variables
Variable Name for a memory object. Variable
names must start with letters and contain letters,
digits, and underscores.
 a, b, i, j, counter, number_of_points, c1234, …
Type What the variable represents. Could be of
integer, floating point number, character, string,
and many others.
 int, float, double, char, …
Declaration Tells compiler about variables and
their type.
 int i,j;
 float sum;
Numeric Types
int integer
float floating point
charater (a very shot
char
integer)
short or short
short integer
int
long or long int long integer
double long floating point
long double very long floating point
Reading Inputs
1. #include <stdio.h>
2. int main()
3. {
4. float a,b;
5. printf("Please input the first number:\n");
6. scanf("%f",&a);
7. printf("Please input the second number:\n");
8. scanf("%f",&b);

9. printf("a+b=%f\n",a+b);
10. printf("a-b=%f\n",a-b);
11. printf("a*b=%f\n",a*b);
12. printf("a/b=%f\n",a/b);
13.}
Assignment
1. /* Arithmetic operators */
2. #include <stdio.h>
3.
4. int main()
5. {
6. int a,c;
7. int b=4;
8. a=3;
9. c=a+b;
10. printf(“Sum: %d + %d -> %d\n”,a,b,c);
11. a++;b--;
12. prinf(“Now a=%d and b=%d\n”,a,b);
13. return 0;
14.}
What is Object Oriented Programming?

Identifying objects and assigning


responsibilities to these objects.
Objects communicate to other
objects by sending messages.
An object is like a Messages are received by the
black box. methods of an object
The internal details
are hidden.

16
What is an object?
Tangible Things as a car, printer, ...
Roles as employee, boss, ...
Incidents as flight, overflow, ...
Interactions as contract, sale, ...
Specifications as colour, shape, …

17
So, what are objects?

An object represents an individual, identifiable item,


unit, or entity, either real or abstract, with a well-defined
role in the problem domain.
Or
An "object" is anything to which a concept applies.
Etc.

18
Why do we care about objects?
Modularity - large software projects can be split up
in smaller pieces.
Reusability - Programs can be assembled from pre-
written software components.
Extensibility - New software components can be
written or developed from existing ones.

19
Example: The Person class
#include<string>
#include<iostream>
class Person{ private
data
char name[20];
int yearOfBirth;

public: public
void displayDetails() { processes
cout << name << " born in "
<< yearOfBirth << endl;
}
//...
};
The two parts of an object
Object = Data + Methods

or to say the same differently:

An object has the responsibility to know and the


responsibility to do.

= +
21
Basic Terminology
Abstraction is the representation of the essential features of
an object. These are ‘encapsulated’ into an abstract data
type.

Encapsulation is the practice of including in an object


everything it needs hidden from other objects. The internal
state is usually not accessible by other objects.

22
Basic Terminology:
Inheritance

Inheritance means that one class inherits the


characteristics of another class.
This is also called a “is a” relationship:

A car is a vehicle

A dog is an animal

A teacher is a person

23
Basic Terminology:
Polymorphism
Polymorphism means “having many forms”.
 It allows different objects to respond to the same
message in different ways, the response specific to the
type of the object.
E.g. the message displayDetails() of the Person class should give
different results when send to a Student object (e.g. the enrolment
number).

24
Basic Terminology:
Aggregation
Aggregation describes a “has a” relationship. One
object is a part of another object.
A car has wheels.

We distinguish between composite aggregation (the


composite “owns” the part) and shared aggregation
(the part is shared by more then one composite).

25
Basic Terminology:
Behaviour and Messages
The most important aspect of an object is its behaviour
(the things it can do).
 A behaviour is initiated by sending a message to the
object (usually by calling a method).

26
The two steps of Object Oriented
Programming

Making Classes: Creating, extending or reusing


abstract data types.

Making Objects interact: Creating objects from


abstract data types and defining their relationships.

27
Historical Notes
C++ owes most to C.
Other ancestors are Simula67
and Algol68. C++ 1987
First versions of C++ in 1980 under the name “C with
classes”. Since 1983 the name C++ is used.
1990: ANSI/ISO 9899 defines a standard for C
1998: ISO/IEC 14882 specifies the standard for C++

28
C++ and C
C is a subset of C++.
Advantages: Existing C libraries can be used, efficient
code can be generated.
But: C++ has the same caveats and problems as C (e.g.
pointer arithmetic,…).
C++ can be used both as a low level and as a high level
language.

29
C++ and Java
Java is a full object oriented language, all code has to go
into classes.
C++ - in contrast - is a hybrid language, capable both of
functional and object oriented programming.

So, C++ is more powerful but also


more difficult to handle than Java.

30
Books
Teach Yourself C++ in 10 minutes,
J. Liberty, SAMS 1999.
C++ - How to program, Deitel & Deitel, Prentice Hall,
2001.
Object Oriented Programming with C++, David
Parson, Letts Educational, London 1997.

31
Websites
A C++ online tutorial:
http://www.cplusplus.com/doc/tutorial/
The C++ FAQ:
http://www.parashift.com/c++-faq-lite
The homepage of Bjarne Stroustrup, the inventor of C++:
http://www.research.att.com/~bs

ny m ore!
y, ma
32 An d man

Vous aimerez peut-être aussi