C with C++Object Oriented Programming Development

y By:

Suryakant Kamble


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


Beginning C Programming

Basic Computer Model Input CPU Output Memory Instructions + Data Von Neumann Architecture .

Programming Languages Machine Languages +1300042774 +1400593419 +1200274027 Assembly Languages LOAD A ADD B STORE C High-Level Languages C=A+B .

c Source File compile hello.o Object File (Machine Languages) Link hello (High-Level Languages) Executable hello.c hello.obj .Creating Programs C libaray Edit hello.

7. printf("hello. "). } y Editing: Turbo C++. int main() 5. /* Hello. y Compiling: Alt + F9 y Executing:Ctrl + F9 Hello.Borland Compiler.1. 8. 4. 9. { 6.h> 3. return 0. #include <stdio. printf("world\n"). World . world program */ 2.

Always ended with a semicolon(. ‡ To include header files containing information about C libraries y Statement A line of code that tells computer to perform some action. printf . y Preprocessing Done before the program is compiled.).Basic Programming Concepts y CommentTo explain what the program is for and why it is the way it is. ‡ Two functions in the hello world program: main. ‡ Invoking functions y Function A module often consisting of a set of statements.

y Modify the hello world program to try out various escape sequences like: ‡ \n: Newline. Position cursor at the beginning of the next line.Escape Sequences y Escape Sequences are used to control printf to do something other than printing characters. Move cursor to the next tab stop. Sound the system bell. ‡ \t: Tab. ‡ \a: Alert. .

Parentheses can be used. + * / % ++ -- Addition Subtraction Multiplication Division Modulus (remainder) Increment Decrement .Arithmetic Operators y To form expressions y Normal order of operations is followed.

return 0.0/3. #include <stdio.7. printf("7/3=%d\n". 7. 8.h> 3. 9.7-3). 13. 4.7/3). printf("7%%3=%d\n".7*3). { 6.0).0/3.7%3). 12. printf("7. printf("7-3=%d\n".7+3). int main() 5. 10. printf("7*3=%d\n".} . 11.0=%f\n". /* Arithmetic operators */ 2. printf("7+3=%d\n".Arithmetic Operators: An Example 1.

Could be of integer. number_of_points. . character. b. char. ‡ a. j. and underscores. ‡ int. ‡ int i. c1234. ‡ float sum. i.Variables y Variable Name for a memory object. counter. floating point number. « y TypeWhat the variable represents. double. digits. and many others. float. « y DeclarationTells compiler about variables and their type. string. Variable names must start with letters and contain letters.j.

Numeric Types int float char integer floating point charater (a very shot integer) short or short short integer int long or long int long integer double long double long floating point very long floating point .

scanf("%f". float a.Reading Inputs 1. int main() 3. #include <stdio. 13.b. 12.&b). printf("a/b=%f\n". 5. .} printf("a+b=%f\n". printf("a*b=%f\n".&a). 7. 6. 8.a/b). printf("Please input the second number:\n").h> 2. printf("Please input the first number:\n"). 11.a+b). 10.a-b). scanf("%f". printf("a-b=%f\n". 9. { 4.a*b).

int main() 5.h> 3. int b=4.c. 12. 4.b.a. 11.a. { 6. /* Arithmetic operators */ 2. printf(³Sum: %d + %d -> %d\n´. #include <stdio.Assignment 1. c=a+b.b--. a=3. return 0. a++. 10.c).b). 14.} . 8. prinf(³Now a=%d and b=%d\n´. 9. 7. 13. int a.

of an object The internal details are hidden.What is Object Oriented Programming? y Identifying objects and assigning responsibilities to these objects. An object is like a black y Messages are received by the methods box. 16 . y Objects communicate to other objects by sending messages.

.What is an object? y Tangible Things y Roles y Incidents y Interactions y Specifications as a car. . as contract... as flight. printer.. as employee. . boss.. as colour. overflow. shape. « 17 ... sale... .

So. unit. either real or abstract. or entity. Or y An "object" is anything to which a concept applies. 18 . Etc. with a well-defined role in the problem domain. identifiable item. what are objects? y An object represents an individual.

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

.. private data public: void displayDetails() { cout << name << " born in " << yearOfBirth << endl. int yearOfBirth. public processes . }. } //.Example: The Person class #include<string> #include<iostream> class Person{ char name[20].

= 21 + .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.

22 .Basic Terminology y Abstraction is the representation of the essential features of an object. These are ¶encapsulated· into an abstract data type. y 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.

This is also called a ´is aµ relationship: A car is a vehicle 23 .Basic Terminology: Inheritance y Inheritance means that one class inherits the characteristics of another class.

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

25 . One object is a part of another object. A car has wheels.Basic Terminology: Aggregation y Aggregation describes a ´has aµ relationship. y We distinguish between composite aggregation (the composite ´ownsµ the part) and shared aggregation (the part is shared by more then one composite).

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

y Making Objects interact: Creating objects from abstract data types and defining their relationships.The two steps of Object Oriented Programming y Making Classes: Creating. 27 . extending or reusing abstract data types.

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

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

C++ is more powerful but also more difficult to handle than Java.is a hybrid language. capable both of functional and object oriented programming. y C++ . So.C++ and Java y Java is a full object oriented language.in contrast . all code has to go into classes. 30 .

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

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

Sign up to vote on this title
UsefulNot useful