Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Size: px
Start display at page:

Download "Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007"

Transcription

1 Intro. Classes Beginning Objected Oriented Programming CIS 15 : Spring 2007

2 Functionalia HW 4 Review. HW Out this week. Today: Linked Lists Overview Unions Introduction to Classes

3 // Create a New Node node * n = new node; n->id = id; // some id Adding a Node to the Middle of (an already built) List node * here; // assume that we want to insert after this pointer Start Here 3 End NULL

4 // Create a New Node node * n = new node; n->id = id; // some id Adding a Node to the Middle of (an already built) List node * here; // assume that we want to insert after this pointer n-> = here->; // (1) here-> = n; // (2) Start Here 3 End NULL

5 // Create a New Node node * n = new node; n->id = id; // some id Adding a Node to the Middle of (an already built) List node * here; // assume that we want to insert after this pointer n-> = here->; // (1) here-> = n; // (2) Start Here 3 (1) End NULL

6 // Create a New Node node * n = new node; n->id = id; // some id Adding a Node to the Middle of (an already built) List node * here; // assume that we want to insert after this pointer n-> = here->; // (1) here-> = n; // (2) Start Here 3 (1) End 1 2 (2) 4 5 NULL

7 Delete a Node in the Middle of the List node * del; // node to delete Start Del End NULL

8 Delete a Node in the Middle of the List node * del; // node to delete node * prev; //(need to know the previous node) prev-> = del->; delete del; Start Prev Del End NULL

9 // Create a New Node Delete a Node in the Middle of the List node * n = new node; n->id = id; // some id What about deleting at the Start and End of the list? node * del; // node to delete node * prev; //(need to know the previous node) prev-> = del->; delete del; Start Prev Del End NULL

10 Write a For Loop that print out all of the ids node * start; node * end; Start End NULL

11 Write a For Loop that print out all of the ids node * start; node * end; for(node * n = start; n!= null; n = n->) cout << n->id; Start End NULL

12 Unions Unions are structs where all of the members reside in the same memory space. union Number short little; int bigger; long even_bigger; double with_decimals;

13 Unions Unions are structs where all of the members reside in the same memory space. union Number short little; int bigger; long even_bigger; 1 byte double with_decimals; Number

14 Unions Unions are structs where all of the members reside in the same memory space. union Number short little; int bigger; long even_bigger; double with_decimals; 1 byte 2 bytes Number

15 Unions Unions are structs where all of the members reside in the same memory space. union Number short little; int bigger; long even_bigger; double with_decimals; 1 byte 2 bytes 4 bytes Number

16 Unions Unions are structs where all of the members reside in the same memory space. union Number short little; 1 byte int bigger; long even_bigger; double with_decimals; 2 bytes 4 bytes 8 bytes Number

17 union Number short little; int bigger; long even_bigger; double with_decimals; Unions You can use a Union just like a struct. Just know that you can only use one member at a time. Number mynum; mynum.even_bigger = ; cout << mynum.bigger; //????????????????

18 Unions What s the point? You can be cheap with memory. And (in C) this is a weak form of object-orientation. Example : Palm OS Event Handler

19 Unions What s the point? You can be cheap with memory. And (in C) this is a weak form of object-orientation. Example : Palm OS Event Handler struct EventType eventsenum etype; Boolean pendown; UInt8 tapcount; Int16 screenx; type of data depends on type of Event Int16 screeny; union data...

20 Unions What s the point? You can be cheap with memory. And (in C) this is a weak form of object-orientation. Example : Palm OS Event Handler struct EventType eventsenum etype; Boolean pendown; UInt8 tapcount; Int16 screenx; Int16 screeny; union data... struct keydown WChar chr; UInt16 keycode; UInt16 modifiers; struct pendown UInt16 tapcount; UInt16 screenx; UInt16 screeny;

21 Introduction to Classes and OOP Classes are the basis for Object Oriented Programming in C++. So far, we ve been doing Procedural Programming in C++. Object Oriented Programming provides better organizational structures for your code - classes = data + functionality. Tenants of Object Oriented Programming 1. Encapsulation 2. Data Hiding 3. Polymorphism 4. Inheritance

22 Encapsulation Like structs, classes provide a way to abstract data (and functions) to a higher-level. Encapsulation abstracts the data into an object. Age Name DOG Bark Breed

23 Data Hiding As a result of encapsulation, the actual implementation of the data is hid from the rest of program and becomes only accessible through an interface. DOG char Name[34]; or getname(); setname(char *); string Name; This allows for flexibility and re-usability of the code/objects that you build.

24 Polymorphism Functions (methods) can have differing behavior depending on the context that they are being called in. This is Polymorphism. bark(int volume); bark(); bark(); bark(); Boxer Poodle

25 Inheritance No longer are Classes singularly defined lumped objects. But they can be related to each other through inheritance - which defines is-a-kind-of relationship. (NOT to be confused by the has-a relationship) Dog Greyhound Boxer Poodle AiboRobot

26 Definition of a Class A class is defined in a similar manner as a struct is. Keyword class to indicate that what follows is a struct.

27 Definition of a Class A class is defined in a similar manner as a struct is. The Name (Tag) of the class follows. This becomes the type that you declare instances of the with.

28 Definition of a Class A class is defined in a similar manner as a struct is. Again, customary to capitalize the first letter of the name of the class.

29 Definition of a Class A class is defined in a similar manner as a struct is. Don t forget the semi-colon!

30 Definition of a Class A class is defined in a similar manner as a struct is. Declarations of your data (and functions - as you will shortly see), contained within the curly-braces. NOTE: Unlike structs - by default the data and the functions of a class are Private.

31 Definition of a Class A class is defined in a similar manner as a struct is. Declarations of your data (and functions - as you will shortly see), contained within the curly-braces. NOTE: Unlike structs - by default the data and the functions of a class are Private. Rectange r1; r1.width = 5.555; COMPILE ERROR!

32 Access Specifiers Use and labels to specify the access to the different data and functions. Even through it is by default private - it is a good idea to explicitly label the private components of the class. double getarea();

33 Access Specifiers Set up public member accessor functions to control access to the private members of the class. double getarea(); The interface to manipulating the object.

34 Access Specifiers Set up public member accessor functions to control access to the private members of the class. double getarea(); Called mutators, or setter functions. Called accessor, or getter functions.

35 Read-Only Member Functions The keyword const can be used to specify that the function will not change any of the data stored in the class. This is helpful to guarantee that there is no future code inadvertently overwrites something.

36 Defining Member Functions Once declared in the class, member functions are defined outside of the class definition. Note the use of the :: scope operator. scope operator (used with the class name) void Rectangle::setWidth(double w) width = w; } void Rectangle::setHeight(double h) height = h; }

37 Defining Member Functions Once declared in the class, member functions are defined outside of the class definition. Note the use of the :: scope operator. void Rectangle::setWidth(double w) width = w; } void Rectangle::setHeight(double h) height = h; } Private member variables are accessible from the function definition.

38 Defining Member Functions Once declared in the class, member functions are defined outside of the class definition. Note the use of the :: scope operator. Use of the const operator in the function definition to specify a constant object and read-only access to the class data. double Rectangle::getArea() const return (width * height); }

39 Defining Member Functions Once declared in the class, member functions are defined outside of the class definition. Note the use of the :: scope operator. double Rectangle::getArea() const return (width * height); } Avoids stale data, defining Area as a resulting operation on the two member variables width and height - as opposed to having declared an area member variable.

40 Defining Member Functions Once declared in the class, member functions are defined outside of the class definition. Note the use of the :: scope operator. Notice that the return type comes on the far right-side of the Rectangle::getArea() declaration. double Rectangle::getArea() const return (width * height); }

41 Instantiation of the Class Using the class-name to declare a variable of the class type is called instantiating the class. The variable is called an object. Definition outside of main() and other functions. Rectangle r1; r1.setwidth(1.0); r1.setheight(4.0); cout << r1.getarea() << endl; Use of the Class occurs in main() or other functions.

42 Instantiation of the Class Using the class-name to declare a variable of the class type is called instantiating the class. The variable is called an object. Object r1 is an instance of the Rectangle class. Rectangle r1; r1.setwidth(1.0); r1.setheight(4.0); cout << r1.getarea() << endl;

43 Instantiation of the Class Using the class-name to declare a variable of the class type is called instantiating the class. The variable is called an object. Access the member functions (methods) via dotnotation. Rectangle r1; r1.setwidth(1.0); r1.setheight(4.0); cout << r1.getarea() << endl;

44 Instantiation of the Class Using the class-name to declare a variable of the class type is called instantiating the class. The variable is called an object. Access the member functions (methods) via dotnotation. Rectangle r1; r1.setwidth(1.0); r1.setheight(4.0); cout << r1.getarea() << endl; Expected output?

45 Instantiation of the Class Rectangle r1; r1.setwidth(1.0); r1.setheight(4.0); cout << r1.getarea() << endl; 4.0

46 Instantiation of the Class Rectangle r2; cout << r2.getarea() << endl; Expected output?

47 Instantiation of the Class r2.width =? r2.height =? Rectangle r2; cout << r2.getarea() << endl;

48 Pointer to a Class Like other variables, and structs, an object can have a pointer that references it indirectly. Rectangle r3; Rectangle * rptr. rptr = &r3; rptr->setwidth(0.2);

49 Rectangle r3; Rectangle * rptr. rptr = &r3; rptr->setwidth(0.2); Pointer to a Class Like other variables, and structs, an object can have a pointer that references it indirectly. Use of the & operator to set the pointer to hold the memory address of the object.

50 Pointer to a Class Like other variables, and structs, an object can have a pointer that references it indirectly. Rectangle r3; Rectangle * rptr. rptr = &r3; rptr->setwidth(0.2); Use of the -> reference operator to access the public members of the class.

51 Dynamically Allocated Objects Objects (like structs) can be dynamically allocated as well. Rectangle * rptr. rptr = new Rectangle; new operator allows for the creation of a dynamically allocated Rectangle object. rptr->setwidth(0.2);

52 Dynamically Allocated Objects What gets dynamically allocated must eventually be freed. Rectangle * rptr. rptr = new Rectangle; rptr->setwidth(0.2);... delete rptr; delete operator frees the memory of the dynamically allocated Rectangle object.

53 Private Data Having the data hidden and private allows for more intelligent and safer handling of the object. Additionally it allows for better code evolution. Keep on getting errors! Negative widths. Need to update the setwidth(...) function. void Rectangle::setWidth(double w) width = w; }

54 Private Data Does the rest of the program need to change? void Rectangle::setWidth(double w) if(width >= 0) width = w; else cout << Width is invalid! << endl; exit(exit_failure); // quits the program } }

55 Private Data void Rectangle::setWidth(double w) if(width >= 0) width = w; else cout << Width is invalid! << endl; width = 0; // error handling } } Less heavy handed. But needs to be documented!

56 Inline Member Functions Very small and simple functions can be declared inline to improve code clarity and possible performance enhancements. void setwidth(double w) if(width >= 0) width = w; } void setheight(double h) if(height >= 0) height = h; }

57 Inline Member Functions The actual call of the function gets replaced with a copy inline code. Instead of the normal use of the call stack and jump in the program code. void setwidth(double w) if(width >= 0) width = w; } void setheight(double h) if(height >= 0) height = h; } Code gets copied.

58 Inline Member Functions The actual call of the function gets replaced with a copy inline code. Instead of the normal use of the call stack and jump in the program code. void setwidth(double w) if(width >= 0) width = w; } void setheight(double h) if(height >= 0) height = h; } Inline functions are a request to the compiler. Code size grows considerably with many inline function calls.

59 Rectangle(); Constructors A constructor is a special member function that gets called when the class is instantiated. Name of the constructor is the same as a the class name.

60 Rectangle(); Constructors A constructor is a special member function that gets called when the class is instantiated. Note the absence of a return type!

61 Rectangle(); Constructors A constructor is a special member function that gets called when the class is instantiated. Rectangle::Rectangle() width = 0; height = 0; } Defined like other member functions. Common use: initialization of object data.

62 Rectangle(); Rectangle::Rectangle() width = 0; height = 0; } Constructors A constructor is a special member function that gets called when the class is instantiated. Rectangle r1; cout << r1.getarea() << endl; What is printed?

63 Rectangle(); Rectangle::Rectangle() width = 0; height = 0; } Constructors A constructor is a special member function that gets called when the class is instantiated. Rectangle r1; cout << r1.getarea() << endl; 0

64 Rectangle(); Rectangle::Rectangle() width = 0; height = 0; } Constructors A constructor is called also when an object is dynamically allocated. Rectangle * rptr; rptr = new Rectangle; cout << r1->getarea() << endl;

65 Constructors Constructors also can have function parameters (useful in initializing member data). Rectangle(double w, double h); Rectangle::Rectangle(double w, double h) width = h; height = h; }

66 Constructors Use of parameters in instantiating the object looks similar to a function call. Rectangle(double w, double h); Rectangle::Rectangle(double w, double h) width = h; height = h; } Rectangle r1(0.1, 10.0); cout << r1.getarea() << endl; What is printed?

67 Constructors Use of parameters in instantiating the object looks similar to a function call. Rectangle(double w, double h); Rectangle::Rectangle(double w, double h) width = h; height = h; } Rectangle r1(0.1, 10.0); cout << r1.getarea() << endl; 1.0

68 Constructors Default values can be set in the definition of the Constructor to create a default constructor. Rectangle(double w, double h); Rectangle::Rectangle(double w = 1.0, double h = 1.0) width = h; height = h; }

69 Constructors Default values can be set in the definition of the Constructor to create a default constructor. Rectangle(double w, double h); Rectangle r1; cout << r1.getarea() << endl; What is printed? Rectangle::Rectangle(double w = 1.0, double h = 1.0) width = h; height = h; }

70 Constructors Default values can be set in the definition of the Constructor to create a default constructor. Rectangle(double w, double h); Rectangle r1; cout << r1.getarea() << endl; 1.0 Rectangle::Rectangle(double w = 1.0, double h = 1.0) width = h; height = h; }

71 Passing parameters in Dynamic Objects Dynamically Allocated Objects can also have parameters passed to the Constructor. Rectangle(double w, double h); Rectangle * rptr; rptr = new Rectangle(2.0, 1.0); cout << rptr->getarea() << endl; What is printed? Rectangle::Rectangle(double w = 1.0, double h = 1.0) width = h; height = h; }

72 Passing parameters in Dynamic Objects Dynamically Allocated Objects can also have parameters passed to the Constructor. Rectangle(double w, double h); Rectangle * rptr; rptr = new Rectangle(2.0, 1.0); cout << rptr->getarea() << endl; 2.0 Rectangle::Rectangle(double w = 1.0, double h = 1.0) width = h; height = h; }

73 Destructors Destructors are member functions that are called when the object is de-allocated. Rectangle(double w, double h); ~Rectangle(); Same as a constructor except declaration uses a ~ to indicate that it is a Destructor.

74 Destructors Destructors are member functions that are called when the object is de-allocated. Rectangle(double w, double h); ~Rectangle(); Definition is the same. Note no parameters are ever used. Rectangle::~Rectangle() cout << Bye bye! << endl; }

75 Destructors Rectangle(double w, double h); ~Rectangle(); Rectangle::~Rectangle() cout << Bye bye! << endl; } int main() Rectangle r1(0.1, 10.0); } cout << r1.getarea() << endl; What is printed? Destructor is called at the end of main().

76 Destructors Rectangle(double w, double h); ~Rectangle(); Rectangle::~Rectangle() cout << Bye bye! << endl; } int main() Rectangle r1(0.1, 10.0); } cout << r1.getarea() << endl; 1.0 Bye bye! Destructor is called at the end of main().

77 Destructors Rectangle(double w, double h); ~Rectangle(); Rectangle::~Rectangle() cout << Bye bye! << endl; } Destructors of dynamically allocated objects is called when the delete operator is used. int main() Rectangle * rptr = new Rectangle(0.1, 10.0); } cout << rptr->getarea() << endl; delete rptr;

78 class Cat Design your own class. 3 Member Variables Constructor Destructor Accessor/Mutator Functions.

Introduction to Classes

Introduction to Classes Introduction to Classes Procedural and Object-Oriented Programming Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects Islamic University of Gaza Faculty of Engineering Computer Engineering Dept Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn how to describe objects and classes and how to define

More information

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming Chapter 13: Introduction to Classes 1 13.1 Procedural and Object-Oriented Programming 2 Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a

More information

Structured Data. CIS 15 : Spring 2007

Structured Data. CIS 15 : Spring 2007 Structured Data CIS 15 : Spring 2007 Functionalia HW4 Part A due this SUNDAY April 1st: 11:59pm Reminder: I do NOT accept LATE HOMEWORK. Today: Dynamic Memory Allocation Allocating Arrays Returning Pointers

More information

System Design and Programming II

System Design and Programming II System Design and Programming II CSCI 194 Section 01 CRN: 10968 Fall 2017 David L. Sylvester, Sr., Assistant Professor Chapter 13 Introduction to Classes Procedural and Object-Oriented Programming Procedural

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

More C++ : Vectors, Classes, Inheritance, Templates

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

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

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

Unit 5: More on Classes/Objects Notes

Unit 5: More on Classes/Objects Notes Unit 5: More on Classes/Objects Notes AP CS A The Difference between Primitive and Object/Reference Data Types First, remember the definition of a variable. A variable is a. So, an obvious question is:

More information

Lecture #1. Introduction to Classes and Objects

Lecture #1. Introduction to Classes and Objects Lecture #1 Introduction to Classes and Objects Topics 1. Abstract Data Types 2. Object-Oriented Programming 3. Introduction to Classes 4. Introduction to Objects 5. Defining Member Functions 6. Constructors

More information

Abstraction in Software Development

Abstraction in Software Development Abstract Data Types Programmer-created data types that specify values that can be stored (type of data) operations that can be done on the values The user of an abstract data type (ADT) does not need to

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

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

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

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

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

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

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

Programming II Lecture 2 Structures and Classes

Programming II Lecture 2 Structures and Classes 3. struct Rectangle { 4. double length; 5. double width; 6. }; 7. int main() 8. { Rectangle R={12.5,7}; 9. cout

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

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

Polymorphism. Zimmer CSCI 330

Polymorphism. Zimmer CSCI 330 Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)

More information

Constants, References

Constants, References CS 246: Software Abstraction and Specification Constants, References Readings: Eckel, Vol. 1 Ch. 8 Constants Ch. 11 References and the Copy Constructor U Waterloo CS246se (Spring 2011) p.1/14 Uses of const

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

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

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24

More information

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula Object-Oriented Programming Lecture 2 Dr Piotr Cybula Encapsulation : data protection code safety and independence better team support with the code separation without «giving

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

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

C10: Garbage Collection and Constructors

C10: Garbage Collection and Constructors CISC 3120 C10: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/5/2018 CUNY Brooklyn College 1 Outline Recap OOP in Java: composition &

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

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University (5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University Key Concepts Function templates Defining classes with member functions The Rule

More information

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations. Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn

More information

Dot and Scope Resolution Operator

Dot and Scope Resolution Operator Dot and Scope Resolution Operator Used to specify "of what thing" they are members Dot operator: Specifies member of particular object Scope resolution operator: Specifies what class the function definition

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

1. the base class s (default) constructor is executed first, 2. followed by the derived class s constructor

1. the base class s (default) constructor is executed first, 2. followed by the derived class s constructor Inheritance & Polymorphism Week 7 Gaddis: Chapter 15 CS 5301 Spring 2017 Inheritance A way to create a new class from an existing class The new class is a specialized version of the existing class Base

More information

CLASSES AND OBJECTS IN JAVA

CLASSES AND OBJECTS IN JAVA Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class

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

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction

More information

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay Classes - 2 Data Processing Course, I. Hrivnacova, IPN Orsay OOP, Classes Reminder Requirements for a Class Class Development Constructor Access Control Modifiers Getters, Setters Keyword this const Member

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)

More information

C++ Classes, Constructor & Object Oriented Programming

C++ Classes, Constructor & Object Oriented Programming C++ Classes, Constructor & Object Oriented Programming Object Oriented Programming Programmer thinks about and defines the attributes and behavior of objects. Often the objects are modeled after real-world

More information

Variables. Data Types.

Variables. Data Types. Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1 C How to Program, 6/e 1 Structures : Aggregate data types are built using elements of other types struct Time { int hour; int minute; Members of the same structure must have unique names. Two different

More information

Object Oriented Pragramming (22316)

Object Oriented Pragramming (22316) Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data

More information

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions. Dr.G.R.Damodaran College of Science (Autonomous, affiliated to the Bharathiar University, recognized by the UGC)Reaccredited at the 'A' Grade Level by the NAAC and ISO 9001:2008 Certified CRISL rated 'A'

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

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli COMS W3101 Programming Language: C++ (Fall 2016) ramana@cs.columbia.edu Lecture-2 Overview of C C++ Functions Structures Pointers Design, difference with C Concepts of Object oriented Programming Concept

More information

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

A student was asked to point out interface elements in this code: Answer: cout. What is wrong? A student was asked to point out interface elements in this code: Answer: cout. What is wrong? Clarification of the concept of INTERFACE The interface we ve been talking about in OOP is not the man-machine

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure that you review all previous exams and make sure you fully understand

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

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object-Oriented Programming (OOP) Fundamental Principles of OOP Object-Oriented Programming (OOP) O b j e c t O r i e n t e d P r o g r a m m i n g 1 Object-oriented programming is the successor of procedural programming. The problem with procedural programming is

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Real Application Design Christian Nastasi http://retis.sssup.it/~lipari http://retis.sssup.it/~chris/cpp Scuola Superiore Sant Anna Pisa March 27, 2012 C. Nastasi (Scuola

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 14: Object Oriented Programming in C++ (ramviyas@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Classes (cont d) More on Classes and Members Group presentations Last time

More information

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Topics 7.1 Abstract Data Types 7.2 Object-Oriented Programming

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Real Application Design Christian Nastasi http://retis.sssup.it/~lipari http://retis.sssup.it/~chris/cpp Scuola Superiore Sant Anna Pisa March 27, 2012 C. Nastasi (Scuola

More information

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University (12-1) OOP: Polymorphism in C++ D & D Chapter 12 Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University Key Concepts Polymorphism virtual functions Virtual function tables

More information

Topic 10: Introduction to OO analysis and design

Topic 10: Introduction to OO analysis and design Topic 10: Introduction to OO analysis and design Learning Outcomes Upon successful completion of this topic you will be able to: design classes define class member variables and functions explain public

More information

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++ No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

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

CSc 328, Spring 2004 Final Examination May 12, 2004

CSc 328, Spring 2004 Final Examination May 12, 2004 Name: CSc 328, Spring 2004 Final Examination May 12, 2004 READ THIS FIRST Fill in your name above. Do not turn this page until you are told to begin. Books, and photocopies of pages from books MAY NOT

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

Object Oriented Programming(OOP).

Object Oriented Programming(OOP). Object Oriented Programming(OOP). OOP terminology: Class : A class is a way to bind data and its associated function together. It allows the data to be hidden. class Crectangle Data members length; breadth;

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

GEA 2017, Week 4. February 21, 2017

GEA 2017, Week 4. February 21, 2017 GEA 2017, Week 4 February 21, 2017 1. Problem 1 After debugging the program through GDB, we can see that an allocated memory buffer has been freed twice. At the time foo(...) gets called in the main function,

More information

CS 1302 Chapter 9 (Review) Object & Classes

CS 1302 Chapter 9 (Review) Object & Classes CS 1302 Chapter 9 (Review) Object & Classes Reference Sections 9.2-9.5, 9.7-9.14 9.2 Defining Classes for Objects 1. A class is a blueprint (or template) for creating objects. A class defines the state

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

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

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

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli COMS W3101 Programming Language: C++ (Fall 2015) ramana@cs.columbia.edu Lecture-2 Overview of C continued C character arrays Functions Structures Pointers C++ string class C++ Design, difference with C

More information

Chapter 15: Object Oriented Programming

Chapter 15: Object Oriented Programming Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright Java Basics Object Orientated Programming in Java Benjamin Kenwright Outline Essential Java Concepts Syntax, Grammar, Formatting, Introduce Object-Orientated Concepts Encapsulation, Abstract Data, OO Languages,

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information

Programming in the large

Programming in the large Inheritance 1 / 28 Programming in the large Software is complex. Three ways we deal with complexity: Abstraction - boiling a concept down to its essential elements, ignoring irrelevant details Decomposition

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

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

C11: Garbage Collection and Constructors

C11: Garbage Collection and Constructors CISC 3120 C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017 CUNY Brooklyn College 1 Outline Recap Project progress and lessons

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

15: Polymorphism & Virtual Functions

15: Polymorphism & Virtual Functions 15: Polymorphism & Virtual Functions 김동원 2003.02.19 Overview virtual function & constructors Destructors and virtual destructors Operator overloading Downcasting Thinking in C++ Page 1 virtual functions

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

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