Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Similar documents
Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

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

Dot and Scope Resolution Operator

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

Introduction to Programming Using Java (98-388)

Data Structures using OOP C++ Lecture 3

Encapsulation in C++


Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

C++ Important Questions with Answers

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Fundamentals of Programming Session 24

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

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

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Introduction Of Classes ( OOPS )

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

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

PIC 10A. Lecture 15: User Defined Classes

CIS 190: C/C++ Programming. Classes in C++

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Classes Ch

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

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

Object Oriented Programming COP3330 / CGS5409

Chapter 4 Defining Classes I

III. Classes (Chap. 3)

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

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

Homework #3 CS2255 Fall 2012

EL2310 Scientific Programming

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

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Software Design and Analysis for Engineers

Abstraction in Software Development

Introduction to Programming session 24

An Introduction to C++

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

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

CS201 Some Important Definitions

Object Oriented Programming in C#

C++ Classes, Constructor & Object Oriented Programming

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Object Oriented Design

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Software and Programming 1

EL2310 Scientific Programming

Programming Languages: Encapsulation

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

CS 247: Software Engineering Principles. ADT Design

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

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

CS 106 Introduction to Computer Science I

IS0020 Program Design and Software Tools Midterm, Fall, 2004

C++ : Object Oriented Features. What makes C++ Object Oriented

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

Methods (example: Methods1.java)

An inline function is one in which the function code replaces the function call directly. Inline class member functions

Classes, Objects, and OOP in Java. June 16, 2017

C++ Inheritance and Encapsulation

Object-Oriented Principles and Practice / C++

Access and Non access Modifiers in Core Java Core Java Tutorial

Classes: A Deeper Look

Classes and Data Abstraction. Topic 5

57:017, Computers in Engineering C++ Classes

Assignment 2: Temperature Class

CS313D: ADVANCED PROGRAMMING LANGUAGE

Classes Nov 3, Ch

EECS168 Exam 3 Review

Lecture 18 Tao Wang 1

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

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

Lethbridge/Laganière 2005 Chapter 9: Architecting and designing software 6

Instantiation of Template class

Imperative Languages!

Bounding Object Instantiations, Part 2

Computer Programming C++ Classes and Objects 6 th Lecture

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

CS 231 Data Structures and Algorithms, Fall 2016

Implementing Abstract Data Types (ADT) using Classes

Review: C++ Basic Concepts. Dr. Yingwu Zhu

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Classes and Operators. Ali Malik

CHAD Language Reference Manual

G52CPP C++ Programming Lecture 9

How to engineer a class to separate its interface from its implementation and encourage reuse.

Defining Your Own Classes

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 10: building large projects, beginning C++, C++ and structs

Functions and Recursion

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Transcription:

Class 15 Object-Oriented Development from Structs to Classes

The difference between structs and classes A class in C++ is basically the same thing as a struct The following are exactly equivalent struct Time int hour, min; ; The members of a struct are public by default. This means that they can be manipulated outside the struct class Time public: int hour, min; ; The members of a class are private by default. This means that they can t be used outside the class. If you want to use them outside the class, you need to use the public access specifier.

The difference between structs and classes: how they are used Structs are used to encapsulate (bundle) data advantage: fewer arguments to pass to functions Classes tend to have data as well as functions The functions allow the user to manipulate the data They protect the data from inappropriate manipulation

Example: Insecure data in a struct struct Time int hour, min; ; int main() Time t; cin >> t.hour >> t.min; anything could be added resulting in an invalid Time object t.min++; this would take a valid minute like 59, and change it to 60, which is invalid a class can protect the data so that this won t happen

Adding a member function to protect the data class Time public: void increment(); private: int hour, min; ; member function -This function is public and can be accessed outside the class. - Declaration is in the class declaration -It will add one minute to the time member data -The data is kept private so that only the class member functions can change it

Calling a member function When you call a function in a class, first you must create an object from this class: Time t; Then you can access the function using the dot operator: t.increment()

The implicit argument t.increment(); When you call a function like this, t is called the implicit argument of the function It is as though t was passed by reference. i.e. the increment function will be able to change t and its data members (t.hour and t.min)

Defining the function The declaration of the function is found within the class. The definition of the function is elsewhere in the code (possibly below main) It needs to use the scope resolution operator :: to indicate the class it belongs to: void Time::increment()...

The definition void Time::increment() if ( min < 59 ) min++; The definition of the function starts with return_type class_name::function_name( parameters) else min = 0; hour++; if ( hour == 24 ) hour = 0; In this function, min belongs to an unknown Time object. If you called the function with t.increment() then the min in the function is t.min

Initializing Data Members The data members of a class are private by default int main() Time t; t.hour = 1; causes compiler error!

Initializing class members using the constructor The constructor is a function with a few unusual qualities its name is the same as the class name it has no return type it gets called whenever you declare an object of the class

Example class Time public: Time(); // Declaration of default constructor: 0 parameters // Will be called whenever a Time object is created with // no arguments Time( int h, int m ); private: ; int hour, min; // Declaration of a constructor with 2 parameters: // hour will be set to h, min will be set to m

Definitions of constructors Time::Time() hour = 0; min = 0; Time :: Time( int h, int m ) hour = h; min = m; These are located outside of the class, like function delcarations

Calling the constructors int main() Time t; calls default constructor Time t2( 1, 30 ); calls second constructor Time tarray[2]; calls default constructor twice ; What happens in memory when this code is executed?

Example: Point Class Write a class Point with two private data members x and y, and two constructors (one default, and the other has two parameters which set the values of x and y) The class will have one function which calculates the distance of the Point from the origin. The main function will get x and y values for a Point from the user, construct the Point, and display the length of the Point from the origin.