Miri Ben-Nissan (Kopel) (2017)

Size: px
Start display at page:

Download "Miri Ben-Nissan (Kopel) (2017)"

Transcription

1 Miri Ben-Nissan (Kopel) (2017)

2 int attributes set of operations Attributes: 4 bytes. Integer numbers. Operations: numerical operators logical operations bit operations I/O operations Data Types define the way you use storage (memory) in the programs you write. Miri Kopel, Bar-Ilan University 2

3 How should we describe a car? attributes operations window engine door wheel Miri Kopel, Bar-Ilan University 3

4 A data type (int, float etc.) is characterized by: (1) A set of values that can be assumed by objects of the type. (2) A set of operations that can be performed on objects of the type. Abstract Data Type (ADT) = user defined data type. Miri Kopel, Bar-Ilan University 4

5 ADT = packaging data with functions to create a new data type. This is also called encapsulation. Built-in types: int, bool, float. User-defined types: stacks, queue, tree, student, text-editor. ADT interfaces provide a list of operations ( what ) rather than an implementation description ( how ). Miri Kopel, Bar-Ilan University 5

6 Car inum_of_doors; iyear_of_manufacturing; bis_automatic; turnon(); lock(); ADT = Car Miri Kopel, Bar-Ilan University 6

7 In C++ we use classes for defining ADTs. The syntax: class ClassName //attributes and operations ; Objects are instances of classes. That is, objects are to classes what variables are to types. A class definition does not allocate storage for any objects. Miri Kopel, Bar-Ilan University 7

8 Example of Object Oriented Analysis N elevators for M floors. Every elevator has M buttons. Miri Kopel, Bar-Ilan University 8

9 When pushing a button in the elevator: button is lighted. elevator arrives. doors are opened and button is turns off. On every floor, except first and last floors, there are two buttons (up & down). When pushing a button on a floor: button is lighted. when the elevator arrives the doors open and the button s light turns off. After a delay, the doors get closed, and the elevator moves to the requested way (if there are any requests). When there are no requests for the elevator it parks in the current floor with closed doors. Miri Kopel, Bar-Ilan University 9

10 elevator elevator button button floor button door floor 2 uses of class functions: What to do Message Method How to do Miri Kopel, Bar-Ilan University 10

11 #include <iostream> class Point public: int x,y; void Show () std::cout<<"x="<<x<<" y="<<y<<std::endl; ; int main () Point p; p.x=15; p.y=10; p.show (); std::cout<<"please enter x and y values: "; std::cin >> p.x >> p.y; p.show (); Miri Kopel, Bar-Ilan University 11

12 The users of the class can clearly see exactly what they can use and what to ignore. The ability to ensure that no client programmer becomes dependent on any part the underlying implementation of a class. public = all member declarations that follows are available to everyone. private = no one can access that member except the creator of the type inside function members of that type. It s a brick wall between the object and the client programmer. Miri Kopel, Bar-Ilan University 12

13 class MyClass public: //Data and methods accessible to any user of //the class. protected: //Data and methods accessible to class //methods, derived classes, and friends only. private: //Data and methods accessible to class //methods and friends only. ; Miri Kopel, Bar-Ilan University 13

14 #include <iostream> class Point private: int m_x, m_y; public: void Set_x(int val) m_x=val; int Get_x() return m_x; void Set_y(int val) m_y=val; int Get_y()return m_y; void Show()std::cout<<"x="<<m_x<< " y="<<m_y<<std::endl; ; Miri Kopel, Bar-Ilan University 14

15 Example 2 (cont.): int main () Point p; //p.m_x=15; -----ERROR p.set_x (15); //p.m_y=10; -----ERROR p.set_y (10); p.show (); p.set_x (17); p.set_y (5); std::cout<<"x= "<< p.get_x() <<" y= <<p.get_y()<<std::endl; Miri Kopel, Bar-Ilan University 15

16 Composition Data members may be objects of built-in types as well as user-defined types. Example 3: #include <iostream> #include Point.h class Line private: Point m_p1, m_p2; //composition public: void SetLine(int x1,int y1,int x2,int y2); void SetLine(const Point& p1,const Point& p2); void Show(); ; Miri Kopel, Bar-Ilan University 16

17 void Line::SetLine(int x1,int y1,int x2,int y2) m_p1.set_x(x1); m_p1.set_y(y1); m_p2.set_x(x2); m_p2.set_y(y2); void Line::SetLine(const Point& p1,const Point& p2) m_p1 = p1; //operator = between 2 Points m_p2 = p2; void Line::Show() std::cout<<"line from: "; m_p1.show(); std::cout<<" To: "; m_p2.show(); Miri Kopel, Bar-Ilan University 17

18 int main () Point p1,p2; p1.set_x(15); p1.set_y(10); p2.set_x(0); p2.set_y(0); Line line1,line2; line1.setline(15,10,7,6); line1.show(); line2.setline(p1,p2); line2.show(); Line from: x=15 y=10 To: x=7 y=6 Line from: x=15 y=10 To: x=0 y=0 Miri Kopel, Bar-Ilan University 18

19 In C we have macro: #define SUM(X,Y) ((X)+(Y)) Implemented by the pre-processor. An inline function is a function whose code gets inserted into the caller's code stream. Like a #define macro, inline functions improve performance by avoiding the overhead of the call itself and (especially!) by the compiler being able to optimize through the call ("procedural integration"). Miri Kopel, Bar-Ilan University 19

20 inline functions (cont.): To define an inline function, you must ordinarily precede the function definition with the inline keyword. It is not necessary inside a class definition. A member function defined within the class definition is taken automatically to be an inline member function. That is, in-class definition of member functions is for small, frequently-used functions. Miri Kopel, Bar-Ilan University 20

21 inline functions (cont.): An inline is just a suggestion to the compiler. When the function is too complicated the compiler may reject the inline request. When the compiler must produce an address of the function, it will always reject our request. Constructors and Destructors may have hidden activities inside them since the class can contain sub-objects whose constructors and destructors must be called. You should consider its efficiency before making them inline. Miri Kopel, Bar-Ilan University 21

22 inline functions (cont.): Since the inline mechanism is done by the compiler (before the linkage), it's usually imperative that the function's definition (the part between the...) be placed in a header file. If you put the inline function's definition into a.cpp file, and if it is called from some other.cpp file, you'll get an "unresolved external" error from the linker. You should consider the information hiding subject in this case. Beware that overuse of inline functions can cause code bloat, which can in turn have a negative performance impact in paging environments. Miri Kopel, Bar-Ilan University 22

23 The class designer can guarantee initialization of every object by providing a special function, called the constructor. If a class has a constructor, the compiler automatically calls that constructor at the point an object is created. The name of the constructor is the same as the name of the class. Like any function, the constructor can have arguments to allow us to specify how an object is created, give it initialization values, and so on. Miri Kopel, Bar-Ilan University 23

24 The destructor is guarantee for cleaning up the object. It is called automatically be the compiler when the object goes out of scope. The syntax for the destructor is similar to that for the constructor: the class name with a leading ~. The destructor never has any arguments because destruction never needs any options. Both the constructor and the destructor have no return values. Miri Kopel, Bar-Ilan University 24

25 A default constructor is one that can be called with no arguments. The default constructor is so important that if (and only if) there are no constructors for a class, the compiler will automatically create one for you. If I declared other Ctor in the class, do I get the Default one too? Miri Kopel, Bar-Ilan University 25

26 #include <iostream> using namespace std; class Point private: int m_x, m_y; public: Point () m_x=0; m_y=0; //default constructor Point (int valx, int valy) m_x=valx; m_y=valy; ~Point () cout<<"goodbye"<<endl; //destructor ; void set_x (int val) m_x=val; int get_x () return m_x; void set_y (int val) m_y=val; int get_y () return m_y; void show ()cout<<"x="<<m_x<<" y="<<m_y<<endl; Miri Kopel, Bar-Ilan University 26

27 Example 3 (cont.): int main () Point p1 (15,10); Point p2; //default point std::cout<<"the first point - "; p1.show(); std::cout<<"the second point - "; p2.show(); The first point - x=15 y=10 The second point - x=0 y=0 GoodBye GoodBye Miri Kopel, Bar-Ilan University 27

28 class Point Example 5: private: int m_x, m_y; public: Point(); Point(int x, int y); ~Point()std::cout<<"Deleting a point...\n"; //the set & get methods as before... ; Point::Point() //default constructor std::cout<< Creating a default point \n"; m_x=0; m_y=0; Point::Point(int x, int y) //constructor with args std::cout<<"creating a point...\n"; m_x=x; m_y=y; 28 Miri Kopel, Bar-Ilan University

29 class Line private: Point m_p1, m_p2; public: Line()std::cout<<"Creating a default line...\n";; Line(int x1,int y1,int x2,int y2); ~Line() std::cout<<"deleting a line...\n"; ; Line::Line(int x1,int y1,int x2,int y2) std::cout<<"creating a line...\n"; //set the x and y values of p1 and p2 //with the arguments x1,y1,x2,y2. //... Miri Kopel, Bar-Ilan University 29

30 int main() Line l1; Line l2(2,5,7,8); Line l1 Point m_p1 int m_x int m_y =0 =0 Line l2 Point m_p1 int m_x int m_y =2 =0 =0 =5 Creating a default point... Creating a default point... Creating a default line... Creating a default point... Creating a default point... Creating a line... Deleting a line... Deleting a point... Deleting a point... Deleting a line... Deleting a point... Deleting a point... Point m_p2 Point m_p2 int m_x =0 int m_x =7 =0 int m_y =0 =0 int m_y =8 Miri Kopel, Bar-Ilan University 30

31 In composition, the constructors are called in the following order: Internal object s constructor. External class s constructor. The destructors are called in the reversed order. Miri Kopel, Bar-Ilan University 31

32 Data members may be object of built-in types as well as user-defined types. When an object is created, the compiler guarantees that constructors for all of its subobjects are called. In case all the sub-objects have default constructors, this is what the compiler automatically calls. QUESTION: How do we initialize class data members that are objects of userdefined types whose constructors require arguments? Miri Kopel, Bar-Ilan University 32

33 ANSWER: Use the member initialization section. That is the part of the constructor after the : following the constructor s parameter list (up to the first ). It s a good habit to always use the member initialization section. Member initialization section only applies to constructors. Miri Kopel, Bar-Ilan University 33

34 class Line private: Point m_p1,m_p2; public: Line(int x1, int y1, int x2, int y2); ~Line(); //destructor ; double Length (); void SetLine (int x1,int x2,int y1,int y2); void Show (); Miri Kopel, Bar-Ilan University 34

35 //~~~~~~~~~~~~~~~~~~~~~~~~ // Constructor with initialization list //~~~~~~~~~~~~~~~~~~~~~~~~ Line::Line(int x1,int y1,int x2,int y2) : m_p1(x1,y1), m_p2(x2,y2) //~~~~~~~~~~~~~~~~~~~~~~~~ // Destructor //~~~~~~~~~~~~~~~~~~~~~~~~ Line::~Line( ) cout<<"see you again soon!\n"; //~~~~~~~~~~~~~~~~~~~~~~~~ // Length //~~~~~~~~~~~~~~~~~~~~~~~~ double Line::Length () return sqrt(pow(m_p1.get_x()-m_p2.get_x(),2) + pow(m_p1.get_y()-m_p2.get_y(),2)); Miri Kopel, Bar-Ilan University 35

36 void Line::SetLine (int x1,int x2,int y1,int y2) m_p1.set_x(x1); m_p1.set_y(y1); m_p2.set_x(x2); m_p2.set_y(y2); void Line::Show() std::cout<<"the first point - "; m_p1.show(); cout<<std::endl; std::cout<<"the second point - "; m_p2.show(); cout<<std::endl; Miri Kopel, Bar-Ilan University 36

37 int main () Point p(15,10); p.show(); Line l(2,90,16,1); l.show(); std::cout<<"l's length is - <<l.length()<<std::endl; x=15 y=10 The first point - x=2 y=90 The second point - x=16 y=1 l's length is See you again soon! GoodBye GoodBye GoodBye 37 Miri Kopel, Bar-Ilan University

38 Stack Heap Global Data Segment Code Segment Miri Kopel, Bar-Ilan University 38

39 Handling passing and returning variables by value during function calls: int f(int x, char c); int g = f(a,b); push b push a call f() add sp, 4 mov g, register a Function arguments Return address Local variables Miri Kopel, Bar-Ilan University 39

40 When you pass an object by value, you create a new object (the passed object inside the function frame) from an existing object (the original object outside the function frame). This is also true when returning an object by value. The compiler s assumption is that you want to perform this creation using a bitcopy. object_1 m_parray object_2 m_parray 1024 Miri Kopel, Bar-Ilan University 40

41 In order to prevent the compiler from doing a bitcopy, you define your own function to be used whenever the compiler needs to make a new object from an existing object. This function is called copy constructor. The single argument to this constructor has to do with the object you re constructing from. The object can t be passed into the constructor by value, because you re trying to define the function that handles passing by value Miri Kopel, Bar-Ilan University 41

42 class String Example 7: private: char* m_str; //... public: String(const char* str=null); String(const String& str); //copy constructor ~String(); //... ; If I declare a C.Ctor only, do I get also the Default Ctor? String::String(const String& str) //str cannot be NULL since it s passed by reference m_str = new char[strlen(str.m_str)+1]; strcpy(m_str,str.m_str); Miri Kopel, Bar-Ilan University 42

43 In some cases, it is necessary for the compiler to create temporary objects. These temporary objects can be created for the following reasons: Result of expression evaluation. Result of expressions using the built-in (not overloaded) logical operators ( and &&). Initializing const references. To store the result of a cast to a user-defined type. To store the return value of a function that returns a user-defined type. Miri Kopel, Bar-Ilan University 43

44 Temporary objects have a lifetime that is defined by their point of creation and the point at which they are destroyed. class MyString public: MyString(const char* str=null); ~MyString(); private: char* m_str; ; Example 8: Miri Kopel, Bar-Ilan University 44

45 MyString::MyString(const char* str1/*=null*/) std::cout<<"creating a string\n"; if(str1) m_str = new char[strlen(str1)+1]; strcpy(m_str,str1); else m_str = NULL; MyString::~MyString() if(m_str) delete[] m_str; m_str = NULL; std::cout<<"deleting a string\n"; Miri Kopel, Bar-Ilan University 45

46 MyString GetString() MyString str2("testing"); return str2; int main() MyString str3( GetString() ); main() GetString() str3 temp str2 m_str Miri Kopel, Bar-Ilan University 46

Initializing and Finalizing Objects

Initializing and Finalizing Objects 4 Initializing and Finalizing Objects 147 Content Initializing and Finalizing Objects 4 Constructors Default Constructor Copy Constructor Destructor 148 Initializing Objects: Constructors Initializing

More information

CS 11 C++ track: lecture 1

CS 11 C++ track: lecture 1 CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account http://www.cs.caltech.edu/cgi-bin/ sysadmin/account_request.cgi Need to know UNIX (Linux) www.its.caltech.edu/its/facilities/labsclusters/

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Advanced Programming & C++ Language

Advanced Programming & C++ Language Advanced Programming & C++ Language ~10~ C++11 new features Ariel University 2018 Dr. Miri (Kopel) Ben-Nissan 2 Evolution of C++ Language What is C++11? 3 C++11 is the ISO C++ standard formally ratified

More information

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 8: Object-Oriented Programming (OOP) 1 Introduction to C++ 2 Overview Additional features compared to C: Object-oriented programming (OOP) Generic programming (template) Many other small changes

More information

Abstract Data Types (ADT) and C++ Classes

Abstract Data Types (ADT) and C++ Classes Abstract Data Types (ADT) and C++ Classes 1-15-2013 Abstract Data Types (ADT) & UML C++ Class definition & implementation constructors, accessors & modifiers overloading operators friend functions HW#1

More information

CS11 Intro C++ Spring 2018 Lecture 1

CS11 Intro C++ Spring 2018 Lecture 1 CS11 Intro C++ Spring 2018 Lecture 1 Welcome to CS11 Intro C++! An introduction to the C++ programming language and tools Prerequisites: CS11 C track, or equivalent experience with a curly-brace language,

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

C++ (classes) Hwansoo Han

C++ (classes) Hwansoo Han C++ (classes) Hwansoo Han Inheritance Relation among classes shape, rectangle, triangle, circle, shape rectangle triangle circle 2 Base Class: shape Members of a class Methods : rotate(), move(), Shape(),

More information

Module Operator Overloading and Type Conversion. Table of Contents

Module Operator Overloading and Type Conversion. Table of Contents 1 Module - 33 Operator Overloading and Type Conversion Table of Contents 1. Introduction 2. Operator Overloading 3. this pointer 4. Overloading Unary Operators 5. Overloading Binary Operators 6. Overloading

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

pointers & references

pointers & references pointers & references 1-22-2013 Inline Functions References & Pointers Arrays & Vectors HW#1 posted due: today Quiz Thursday, 1/24 // point.h #ifndef POINT_H_ #define POINT_H_ #include using

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Recharge (int, int, int); //constructor declared void disply();

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

CS11 Introduction to C++ Fall Lecture 1

CS11 Introduction to C++ Fall Lecture 1 CS11 Introduction to C++ Fall 2006-2007 Lecture 1 Welcome! 8 Lectures (~1 hour) Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7 Lab Assignments on course website Available on Monday

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

More Tutorial on C++:

More Tutorial on C++: More Tutorial on C++: OBJECT POINTERS Accessing members of an object by using the dot operator. class D { int j; void set_j(int n); int mul(); ; D ob; ob.set_j(4); cout

More information

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

QUIZ How do we implement run-time constants and. compile-time constants inside classes? QUIZ How do we implement run-time constants and compile-time constants inside classes? Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object

More information

Java Classes & Primitive Types

Java Classes & Primitive Types Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.)

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

SFU CMPT Topic: Classes

SFU CMPT Topic: Classes SFU CMPT-212 2008-1 1 Topic: Classes SFU CMPT-212 2008-1 Topic: Classes Ján Maňuch E-mail: jmanuch@sfu.ca Friday 15 th February, 2008 SFU CMPT-212 2008-1 2 Topic: Classes Encapsulation Using global variables

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

The Class Construct Part 2

The Class Construct Part 2 The Class Construct Part 2 Lecture 24 Sections 7.7-7.9 Robb T. Koether Hampden-Sydney College Mon, Oct 29, 2018 Robb T. Koether (Hampden-Sydney College) The Class Construct Part 2 Mon, Oct 29, 2018 1 /

More information

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING Classes and Objects So far you have explored the structure of a simple program that starts execution at main() and enables you to declare local and global variables and constants and branch your execution

More information

Classes in C++98 and C++11

Classes in C++98 and C++11 Classes in C++98 and C++11 January 10, 2018 Brian A. Malloy Slide 1 of 38 1. When we refer to C++98, we are referring to C++98 and C++03, since they differ only slightly. C++98 contained 3 types of constructors,

More information

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

the gamedesigninitiative at cornell university Lecture 7 C++ Overview Lecture 7 Lecture 7 So You Think You Know C++ Most of you are experienced Java programmers Both in 2110 and several upper-level courses If you saw C++, was likely in a systems course Java was based on

More information

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017 Next week s homework Classes: Methods, Constructors, Destructors and Assignment Read Chapter 7 Your next quiz will be on Chapter 7 of the textbook For : COP 3330. Object oriented Programming (Using C++)

More information

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Classes Chapter 4 Classes and Objects Data Hiding and Encapsulation Function in a Class Using Objects Static Class members Classes Class represents a group of Similar objects A class is a way to bind the

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

C++ Constructor Insanity

C++ Constructor Insanity C++ Constructor Insanity CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts Chapter 11 Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

Ch. 11: References & the Copy-Constructor. - continued -

Ch. 11: References & the Copy-Constructor. - continued - Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed

More information

PHY4321 Summary Notes

PHY4321 Summary Notes PHY4321 Summary Notes The next few pages contain some helpful notes that summarize some of the more useful material from the lecture notes. Be aware, though, that this is not a complete set and doesn t

More information

Today s lecture. CS 314 fall 01 C++ 1, page 1

Today s lecture. CS 314 fall 01 C++ 1, page 1 Today s lecture Midterm Thursday, October 25, 6:10-7:30pm general information, conflicts Object oriented programming Abstract data types (ADT) Object oriented design C++ classes CS 314 fall 01 C++ 1, page

More information

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently. OBJECTS An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently. Objects that work in associations with each others. Objects that frequently

More information

W3101: Programming Languages C++ Ramana Isukapalli

W3101: Programming Languages C++ Ramana Isukapalli Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical

More information

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 14. Dr Jason Atkin G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 12: Classes and Data Abstraction Objectives In this chapter, you will: Learn about classes Learn about private, protected,

More information

Structs. Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: Memory Layout

Structs. Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: Memory Layout Structs (C,C++) 2 Structs Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec int i; int a[3]; int *p; Memory Layout

More information

Java Classes & Primitive Types

Java Classes & Primitive Types Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.)

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Constructors & Destructors

Constructors & Destructors Constructors & Destructors Constructor It is a member function which initializes a class. A constructor has: (i) the same name as the class itself (ii) no return type class rectangle private: float height;

More information

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez! C++ Mini-Course Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion C Rulez! C++ Rulez! C++ Mini-Course Part 1: Mechanics C++ is a

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

Constructor - example

Constructor - example Constructors A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked whenever

More information

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator C++ Addendum: Inheritance of Special Member Functions Constructors Destructor Construction and Destruction Order Assignment Operator What s s Not Inherited? The following methods are not inherited: Constructors

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Function Definitions - Function Prototypes - Data

More information

Creating an object Instance variables

Creating an object Instance variables Introduction to Objects: Semantics and Syntax Defining i an object Creating an object Instance variables Instance methods What is OOP? Object-oriented programming (constructing software using objects)

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

CS

CS CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.. COMP6771 Advanced C++ Programming Week 5 Part Two: Dynamic Memory Management 2016 www.cse.unsw.edu.au/ cs6771 2.. Revisited 1 #include 2 3 struct X { 4 X() { std::cout

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 14: Object Oriented Programming in C++ (fpokorny@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Wrap Up Introduction to Object Oriented Paradigm Classes More on Classes

More information

Implementing Subprograms

Implementing Subprograms Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

Advanced Systems Programming

Advanced Systems Programming Advanced Systems Programming Introduction to C++ Martin Küttler September 19, 2017 1 / 18 About this presentation This presentation is not about learning programming or every C++ feature. It is a short

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language

More information

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE PART A UNIT I 1. Differentiate object oriented programming from procedure oriented programming. 2. Define abstraction and encapsulation. 3. Differentiate

More information

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods. Classes: Methods, Constructors, Destructors and Assignment For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Classes: Member functions // classes

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Introduction to the C programming language

Introduction to the C programming language Introduction to the C programming language From C to C++: Stack and Queue Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 23, 2010 Outline 1 From struct to classes

More information

Evolution of Programming Languages

Evolution of Programming Languages Evolution of Programming Languages 40's machine level raw binary 50's assembly language names for instructions and addresses very specific to each machine 60's high-level languages: Fortran, Cobol, Algol,

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington CSE 333 Lecture 11 - constructor insanity Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia Exercises: - New exercise out today, due Monday morning

More information

Scope. Scope is such an important thing that we ll review what we know about scope now:

Scope. Scope is such an important thing that we ll review what we know about scope now: Scope Scope is such an important thing that we ll review what we know about scope now: Local (block) scope: A name declared within a block is accessible only within that block and blocks enclosed by it,

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments Reference and Pointer Pitfalls Function Declarations Never return a reference or a pointer to a local object. The return value will refer to memory that has been deallocated and will be reused on the next

More information

Introducing C++ to Java Programmers

Introducing C++ to Java Programmers Introducing C++ to Java Programmers by Kip Irvine updated 2/27/2003 1 Philosophy of C++ Bjarne Stroustrup invented C++ in the early 1980's at Bell Laboratories First called "C with classes" Design Goals:

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

STRUCTURING OF PROGRAM

STRUCTURING OF PROGRAM Unit III MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of Data Abstraction? (a) Reduce Complexity (c) Parallelism Unit III 3.1 (b) Binds together code and data (d) None of the

More information

An introduction to Java II

An introduction to Java II An introduction to Java II Bruce Eckel, Thinking in Java, 4th edition, PrenticeHall, New Jersey, cf. http://mindview.net/books/tij4 jvo@ualg.pt José Valente de Oliveira 4-1 Java: Generalities A little

More information

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli Lecture-5 Miscellaneous topics Templates W3101: Programming Languages C++ Miscellaneous topics Miscellaneous topics const member functions, const arguments Function overloading and function overriding

More information