Cpt S 122 Data Structures Introduction to C

  • Slides: 23
Download presentation
Cpt S 122 – Data Structures Introduction to C++ Part -I Nirmalya Roy School

Cpt S 122 – Data Structures Introduction to C++ Part -I Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Topics n n n Introduction Object Oriented Programming First Program in C++: Printing a

Topics n n n Introduction Object Oriented Programming First Program in C++: Printing a Line of Text Another C++ Program: Adding Integers Another C++ Program: Using namespace

Introduction n Programs that employ the basic concepts of objectoriented programming. ¡ ¡ n

Introduction n Programs that employ the basic concepts of objectoriented programming. ¡ ¡ n n Typically, consist of function main and one or more classes, each containing data members and member functions. Simple, well-engineered framework for organizing object-oriented programs in C++ programming facilitates a disciplined approach to program design.

What is an Object Oriented Programming? n What is a procedure-oriented programming? ¡ ¡

What is an Object Oriented Programming? n What is a procedure-oriented programming? ¡ ¡ Example: COBOL, FORTRAN and C etc. Problem is viewed as a sequence of things to be done n ¡ ¡ ¡ Example: Reading, calculating and printing A number of functions are written to accomplish this task The primary focus is on functions Very little attention is given to the data that are being used by functions n n What happens to the data? How are they affected by the functions that work on them?

Procedure-Oriented Programming n Multi- function program, many important data items are placed as global

Procedure-Oriented Programming n Multi- function program, many important data items are placed as global ¡ ¡ They may be accessed by all the functions Each function may have its own local data Global Data Function 1 Function 2 Function 3 Local Data Relationship of data and functions in procedural programming

Procedure-Oriented Programming (Cont. ) n What are the disadvantages? ¡ ¡ n n n

Procedure-Oriented Programming (Cont. ) n What are the disadvantages? ¡ ¡ n n n Global data are more vulnerable to an inadvertent change by a function It is very difficult to identify what data is used by which function in a large program In case we need to revise an external data structure, we should also revise all functions that access the data Opportunity for bugs to creep in Large programs are divided into smaller programs known as functions Most of the functions share global data Data move openly around the system from function to function

What is Object-Oriented Programming(OOP) n n n Treats data as a critical element in

What is Object-Oriented Programming(OOP) n n n Treats data as a critical element in the program development Does not allow it to flow freely around the system Ties data more closely to the functions that operate on it Protects it from accidental modifications from outside functions OOP allows us to decompose a problem ¡ ¡ Into a number of entities called objects Builds data and functions around those entities

Object-Oriented Programming(Cont) n n Data of an object can be accessed only by the

Object-Oriented Programming(Cont) n n Data of an object can be accessed only by the functions associated with that object Functions of one object can access the functions of other objects Object A Object B Data Functions Object C Functions Data

Object-Oriented Programming(Cont) n Striking features of object-oriented programming ¡ ¡ ¡ ¡ Emphasis on

Object-Oriented Programming(Cont) n Striking features of object-oriented programming ¡ ¡ ¡ ¡ Emphasis on data rather than procedure Programs are divided into what are known as objects Data structure are designed such that they characterize the objects Functions that operate on the data of an object are tied together in the data structure Data is hidden and cannot be accessed by external functions Objects may communicate through each other through functions New data and functions can be easily added whenever necessary

What is an Object? n n n Objects contain data and code to manipulate

What is an Object? n n n Objects contain data and code to manipulate the data The entire set of data and code of an object can be made a user-defined data type with the help of a class Objects are variable of type class Each object is associated with data of type class Once a class has been defined, we can create any number of objects belonging to that class ¡ ¡ ¡ A class is thus a collection of objects of similar types For example, mango, apple & orange are member of the class fruit If fruit has been defined as a class, then n fruit mango; will create an object mango belonging to the class fruit.

C++ Program: Prints a Line of Text n Simple program that prints a line

C++ Program: Prints a Line of Text n Simple program that prints a line of text.

First Program in C++: Printing a Line of Text (Cont. ) n n n

First Program in C++: Printing a Line of Text (Cont. ) n n n A preprocessor directive is a message to the C++ preprocessor. Lines that begin with # are processed by the preprocessor before the program is compiled. #include <iostream> notifies the preprocessor to include in the program the contents of the input/output stream header file <iostream>. ¡ Must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output.

First Program in C++: Printing a Line of Text (Cont. ) n When a

First Program in C++: Printing a Line of Text (Cont. ) n When a cout statement executes, it sends a stream of characters to the standard output stream object ¡ n The std: : before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include <iostream>. ¡ ¡ n std: : cout; normally “connected” to the screen. The notation std: : cout specifies that we are using a name, in this case cout, that belongs to “namespace” std. The names cin (the standard input stream) and cerr (the standard error stream) also belong to namespace std. The << operator is referred to as the stream insertion operator. ¡ The value to the operator’s right, the right operand, is inserted in the output stream.

Another C++ Program: Adding Integers n The input stream object std: : cin and

Another C++ Program: Adding Integers n The input stream object std: : cin and the stream extraction operator, >>, can be used to obtain data from the user at the keyboard.

C++ Program: Adding Integers

C++ Program: Adding Integers

Another C++ Program: Adding Integers (Cont. ) n n A prompt directs the user

Another C++ Program: Adding Integers (Cont. ) n n A prompt directs the user to take a specific action. A cin statement uses the input stream object cin (of namespace std) ¡ n the stream extraction operator, >>, is used to obtain a value from the keyboard. Using the stream extraction operator with std: : cin takes character input from the standard input stream (the keyboard).

Another C++ Program: Adding Integers (Cont. ) n n std: : endl is a

Another C++ Program: Adding Integers (Cont. ) n n std: : endl is a so-called stream manipulator. The name endl is an abbreviation for “end line” ¡ n belongs to namespace std. The std: : endl stream manipulator outputs a newline, then “flushes the output buffer. ” ¡ ¡ This simply means that, on some systems where outputs accumulate in the machine until there are enough to “make it worthwhile” to display them on the screen, std: : endl forces any accumulated outputs to be displayed at that moment. This can be important when the outputs are prompting the user for an action, such as entering data.

Another C++ Program: Adding Integers (Cont. ) n n Using multiple stream insertion operators

Another C++ Program: Adding Integers (Cont. ) n n Using multiple stream insertion operators (<<) in a single statement is referred to as concatenating, chaining or cascading stream insertion operations. Calculations can also be performed in output statements.

Arithmetic n C++ applies the operators in arithmetic expressions in a precise sequence determined

Arithmetic n C++ applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence ¡ generally the same as those followed in algebra.

Another C++ Program

Another C++ Program

Another C++ Program

Another C++ Program

Using namespace n using declarations ¡ n Once we insert these using declarations, we

Using namespace n using declarations ¡ n Once we insert these using declarations, we can write ¡ ¡ ¡ n eliminate the need to repeat the std: : prefix as we did in earlier programs. cout instead of std: : cout, cin instead of std: : cin and endl instead of std: : endl Many programmers prefer to use the declaration using namespace std; ¡ enables a program to use all the names in any standard C++ header file (such as <iostream>) that a program might include.

Conclusion n What is an object-oriented programming? Using namespace A basic program in C++

Conclusion n What is an object-oriented programming? Using namespace A basic program in C++ using cin and cout