Docsity

introduction to programming in c++, Lecture notes of Object Oriented Programming

Partial preview of the text

Download introduction to programming in c++ and more Lecture notes Object Oriented Programming in PDF only on Docsity!

CENG 707

Data Structures and Algorithms

Yusuf Sahillioğlu

Department of Computer Engineering

Middle East Technical University

CENG 707

Instructor: Yusuf Sahillioğlu

Office: B

Email : [email protected]

Lecture Hours:

  • (^) Thursday 18:00 – 21:00 (A101)

Web Page: http://www.ceng.metu.edu.tr/~ys/ceng707-ds

Thanks to Prof. Nihan Kesim Çiçekli for helping with the

course lecture notes.

Grading

  • (^) Midterm: 35 %
  • (^) Final Exam: 35 %
  • (^) Programming Assignments: 30% (3 x 10%)
  • (^) All assignments are to be your own work. No group projects or

assignments are allowed.

  • (^) Exams are closed-book.

Course Outline

  • (^) Overview of object-oriented programming with

C++ [chapter 1]

  • (^) Algorithm analysis [chapter 2]
  • (^) Sorting [chapter 7]
  • (^) Lists, stacks, queues [chapter 3]
  • (^) Trees [chapter 4]
  • (^) Priority queues [chapter 6]
  • (^) Hashing [chapter 5]
  • (^) Graphs [chapter 9]

Motivational Example

  • (^) All these structures are there to efficiently store

and process data

  • (^) Even a simple data structure you already know

may be very useful if used in the right context

  • (^) Store nxn entries of an N-matrix in an array
    • (^) Huge profit considering n=1 million, usual case
    • (^) Column 1, column n, diagonal (remaining)
    • (^) 3n-
    • (^) Implement set(i,j,v) to update A[] accordingly

Motivational Example

  • (^) All these structures are there to efficiently store and process data
  • (^) Even a simple data structure you already know may be very useful if used in the right context
  • (^) Store nxn entries of an N-matrix in an array
    • (^) Huge profit considering n=1 million, usual case
    • (^) Column 1, column n, diagonal (remaining)
    • (^) 3n-
    • (^) if (j==1) A[i] = v; else if (j==n) A[n+i] = v; else if (i==j) A[2n+i-1] = v;

Object Oriented Programming

 (^) The emphasis is on creating a set of tools which can be used cleanly, with a minimum knowledge about implementation in the user’s driver files. The following concepts are relevant to accomplishing clean interface:

1. Data Abstraction

  • (^) Providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details - (^) TV: you can turn on and off, change the channel, adjust the volume, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen. - sort: you can sort an array with this C++ call, BUT you do not know the algorithm.
  • (^) In C++, we use classes to define our own abstract data types (ADT).

Object Oriented Programming

1. Data Abstraction

  • (^) Example: The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that the user doesn't need to know about, but is needed for the class to operate properly.

Object Oriented Programming

3. Encapsulation

  • (^) bundling of data with the methods (or other functions) operating on that data (implementation of the methods).

C++ Techniques

 Relevant techniques include:

1. C++ classes, with private and public members

2. Function and operator name overloading to give

"natural" function calls

3. Templates to allow the same code to be used on

a variety of different data types

4. A clean built-in I/O interface, which itself

involves overloading the input and output

operators

 Learning these techniques is much of what C++

is all about.

A Basic C++ Program

cout << "Enter a real number: " << endl; Here, you don't need to understand how cout displays the text on the user's screen. You need to only know the public interface and the underlying implementation of cout is free to change. //Data Abstraction In C++, all I/O is done by classes. A class is set up to handle input and output streams. Output to the screen is handled by the stream with standard name cout. This is a variable of class ostream. Similarly for cin.

A Basic C++ Program

// second C++ program #include using namespace std; int main(){ int a=23; int b=34; cout << "Enter two integers:" << endl; cin >> a >> b; cout << endl; cout << “a + b =“ << a+b << endl; return 0; }

A Basic C++ Program

// third C++ program #include #include using namespace std; int main(){ double a=15.2; double b=34.3434343; cout << fixed << showpoint; cout << setprecision(2); //2 digits after the dot cout << setw(6) << a << endl; //15. cout << setw(7) << b << endl; //34. return 0; }

Classes and Objects

  • (^) Class: a type definition that includes both
    • (^) data properties, and
    • (^) operations permitted on that data
  • (^) Object: a variable that
    • (^) is declared to be of some Class
    • (^) therefore includes both data and operations for that data
  • (^) Appropriate usage: “A variable is an instance of a type.” “An object is an instance of a class.”