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

Similar documents
Classes: A Deeper Look

Introduction to Programming session 24

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

clarity. In the first form, the access specifier public : is needed to {

Classes and Data Abstraction

57:017, Computers in Engineering C++ Classes

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Chapter 16: Classes and Data Abstraction

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

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

Fundamentals of Programming Session 24

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

Computer Programming with C++ (21)

IS 0020 Program Design and Software Tools

Chapter 9 Classes : A Deeper Look, Part 1


More C++ Classes. Systems Programming

Classes and Objects. Types and Classes. Example. Object Oriented Programming

Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10

Preview 11/1/2017. Constant Objects and Member Functions. Constant Objects and Member Functions. Constant Objects and Member Functions

Chapter 17 - C++ Classes: Part II

A Deeper Look at Classes

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

C++ Classes, Constructor & Object Oriented Programming

Classes and Data Abstraction. Topic 5

OBJECT ORIENTED PROGRAMMING USING C++

Chapter 6: Classes and Data Abstraction

10. Abstract Data Types

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

Angela Chih-Wei Tang Visual Communications Lab Department of Communication Engineering National Central University JhongLi, Taiwan.

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

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

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

III. Classes (Chap. 3)

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

IS 0020 Program Design and Software Tools

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

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

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Abstraction in Software Development

Object Oriented Design

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

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

Classes: A Deeper Look. Systems Programming

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

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

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

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

IS 0020 Program Design and Software Tools

Inheritance, and Polymorphism.

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

Introduction Of Classes ( OOPS )

Constructor - example

Class. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE

Evolution of Programming Languages

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

Where do we go from here?

Object Oriented Design

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

Polymorphism Part 1 1

Introduction to Classes

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Object Oriented Programming. Solved MCQs - Part 2

Classes: A Deeper Look, Part 2

Computer Programming Class Members 9 th Lecture

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

Classes and Objects. Class scope: - private members are only accessible by the class methods.

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

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

CGS 2405 Advanced Programming with C++ Course Justification

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

2. It is possible for a structure variable to be a member of another structure variable.

Fast Introduction to Object Oriented Programming and C++

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

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Chapter 10 Introduction to Classes

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Object Oriented Programming

SFU CMPT Topic: Classes

Object-Oriented Programming

The University Of Michigan. EECS402 Lecture 06. Andrew M. Morgan. Savitch Ch. 6 Intro To OOP Classes Objects ADTs.

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

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

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

Polymorphism. Zimmer CSCI 330

AN OVERVIEW OF C++ 1

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

CS201- Introduction to Programming Current Quizzes

Lecture 18 Tao Wang 1

CLASSES AND OBJECTS IN JAVA

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

Transcription:

Preview Object Oriented Programming with C++ Class Members Inline Functions vs. Regular Functions Constructors & Destructors Initializing class Objects with Constructors C++ Programming Language C Programming Language Object Oriented Paradigm Imperative Paradigm C++ is a superset of C 1 2 Imperative Programming action oriented: a computation is viewed as a sequence of actions (instructions). In this paradigm, a program is assumed to be a sequence of detailed statements (commands), instructing the computer exactly how a task is to be accomplished. The word imperative means command. - Sequential imperatives effect changes to the contents of internal storage locations by assignments. This paradigm promotes sharp distinctions between program and data. They are heavily concerned with variables tied to main storage location and with assignment of values to these variables. Supported languages - Fortran (scientific calculation), Algol60, Pascal (teaching language), C (UNIX), COBOL. Object-Oriented Programming Attempt of grouping variables (data) and procedures (operations) together - class Individual data objects are defined by modules of code, called class, which encapsulate data declarations and functionalities (algorithms) for the operation. OO languages Simila, C++, Java, C# 3 4 Goal : Kick a soccer ball with procedural programming 1. Call a function called leg to kick the soccer ball 2. Kick the soccer ball Goal : Kick a soccer ball with object-oriented programming 1. Create a human object to kick the ball 2. Kick the soccer ball 5 6 1

The characteristics of OO programming 1. Information hiding (encapsulation) Classes in C++ are a generalization of records, called structures (struct) in C and C++. 2. Data abstraction 3. Message passing (polymorphism) 4. Dynamic binding 5. Inheritance A class in C++ is an encapsulation of data members and functions that manipulate the data. 7 8 (Members in a Class) class Time Private: int minutes; int seconds; Public: Time (int, int, int) ~Time(); void printtime(); Types of Members in a Class Public members can be accessed by any function in the program. Private members can be accessed only by member functions (and friends) Protected members can be accessed by member functions and to derived classes inheritance 9 10 (Public, Private, and Protected Member ) class Time Time()hour=min=sec=0;; void printtime(time &); ~Time();; ; Data Members The data members can be of a primitive data type, a pointer type, a structured data type or a class type. 11 12 2

Function Members Function can be defined inside the class or outside the class. If a function is defined outside a class structure, the function prototype must be in the class. If a function is defined inside the class, the system considers it to be an inline function. We can define an inline function outside the class using the inline keyword What is an inline function? An inline function is a programming language construct used to suggest to a compiler that a particular function be subjected to in-line expansion. When performed, an in-line expansion inserts the complete body of the function at every context where that function is called. 13 14 Function A St1; St2; Inline expansion FunctionB statement1; Do inline-functions improve performance? call FunctionA; compile statement2; call FunctionA; 15 16 Do inline-functions improve performance? An inline function might make the code faster: Procedural integration by inline function might remove a bunch of unnecessary function calls An inline functions might make the code slower: Too much in-line expansion might cause code bloat, which might cause "thrashing" on demand-paged virtual-memory systems. A Member Function Defined in a Class class Time Time()hour = min = sec = 0;; void settime(int h int m int s) hour = h; min = m; sec = s; ; void printtime(time &t); ~Time();; ; 17 18 3

A Member Function Defined outside a Class A Member Function Defined outside a Class Return_Type Dfdf dfdf Class_Name:: FunctionName (Parameter_List) Statements class Time Time(); void printtime(time &); ~Time(); ; void Time::setTime(int h, int m, int s) hour = h; min = m; sec = s; Time::Time() hour = min = sec = 0; void Time::printTime(Time &t) cout << t.hour << ":" << t.min << ":" << t.sec; 19 20 Constructors and Destructors in a Class Constructor and Destructor in a Class Constructor A Constructor is a special function which exists in every class without any return type. It is used for memory allocation for each object. A constructor has the same name as the class. A constructor is called automatically when an instance of a class is created. If a constructor is not explicitly defined by a programmer, C++ invokes a default constructor, which allocates memory for the object, but without any initialization. Destructor A Destructor is usually used to deallocate memory and perform other cleanup for a class object and its class members when the object is destroyed. A destructor has same name as the class, but is preceded by a ~. A destructor is called automatically to clean up just before the lifetime of an object ends if the object is created statically. If no user-defined destructor exists for a class, the compiler implicitly declares a destructor. 21 22 Accessing Class Member There are two ways to access members of an object. Through the dot operator Time Now(); //create a instance of the class Time Now.SetTime(1, 2, 3); // call public member function Through a pointer Time Now(); //create a instance of the class Time Time *TPtr; //create a pointer to a Time object TPtr = Now; //Assign pointer to the reference to Now Tptr->SetTime(1, 2, 3); // call public member function void main() Time now; int h, m, s; cout << "What's the current time (hour min sec)?"; cin >> h >> m >> s; cout << endl; now.settime(h, m, s); now.printtime(now); 23 24 4

// What is wrong with the following program? void main () Time now; cout << "What's the current time? (hour min sec)"; cin >> now.hour >> now.min >> now.sec; cout << endl; now.printtime(now); Initialize an object using a constructor without any parameters Initialize an object using a constructor with one or more parameters Without default arguments with constructor Using default arguments with constructor 25 26 (Without Default Arguments) // initialize an object with a constructor class TwoNum int one; int two; int add(int x, int y) return (x + y);; Two_Num(int a, int b) one = a; two = b;; ; void main() TwoNum x(2,3); cout << x.one << x.two << endl; (With Default Arguments) // time2.h // Declaration of the Time class. // Member functions are defined in time2.cpp #ifndef TIME2_H #define TIME2_H // Time abstract data type definition class Time Time( int = 0, int = 0, int = 0 ); // default arguments void settime( int, int, int ); // set hour, minute, second void printmilitary(); // print military time format void printstandard(); // print standard time format // 0-23 int minute; // 0-59 int second; // 0-59 ; #endif 27 28 // time2.cpp // Member function definitions for Time class. #include "time2.h" // Time constructor initializes each data member to zero. // Ensures all Time objects start in a consistent state. Time::Time( int hr, int min, int sec ) settime( hr, min, sec ); // Set a new Time value using military time. Perform validity // checks on the data values. Set invalid values to zero. void Time::setTime( int h, int m, int s ) hour = ( h >= 0 && h < 24 )? h : 0; minute = ( m >= 0 && m < 60 )? m : 0; second = ( s >= 0 && s < 60 )? s : 0; // Time.cpp // Demonstrating a default constructor // function for class Time. #include "time2.h" int main() Time t1, // all arguments defaulted t2(2), // minute and second defaulted t3(21, 34), // second defaulted t4(12, 25, 42), // all values specified t5(27, 74, 99); // all bad values specified t1.printmilitary(); cout << endl; t1.printstandard(); cout << endl; // Print Time in military format void Time::printMilitary() cout << ( hour < 10? "0" : "" ) << hour << ":" << ( minute < 10? "0" : "" ) << minute; // Print Time in standard format void Time::printStandard() cout << ( ( hour == 0 hour == 12 )? 12 : hour % 12 ) << ":" << ( minute < 10? "0" : "" ) << minute << ":" << ( second < 10? "0" : "" ) << second << ( hour < 12? " AM" : " PM" ); t2.printmilitary(); cout << endl; t2.printstandard(); cout << endl; t3.printmilitary(); cout << endl; t3.printstandard(); cout << endl; t4.printmilitary(); cout << endl; t4.printstandard(); cout << endl; t5.printmilitary(); cout << endl; t5.printstandard(); cout << endl; return 0; 29 30 5