Chapter 2 Objects and Classes

Slides:



Advertisements
Similar presentations
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Advertisements

Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Operator Overloading and Type Conversions
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Java and C++, The Difference An introduction Unit - 00.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Overview of C++ Polymorphism
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 2 Objects and Classes
Motivation for Generic Programming in C++
Eine By: Avinash Reddy 09/29/2016.
C ++ MULTIPLE CHOICE QUESTION
Pointers and Dynamic Arrays
Classes C++ representation of an object
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Object-Oriented Programming (OOP) Lecture No. 45
Chapter 1 C++ Basics Review
Lecture 13.
Review Bernard Chen Spring 2006.
Review: Two Programming Paradigms
Object Lifetime and Dynamic Objects
Constructor & Destructor
Object-Oriented Programming Using C++
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS212: Object Oriented Analysis and Design
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CMSC 202 Templates.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Operator overloading Dr. Bhargavi Goswami
Lists - I The List ADT.
Objects Managing a Resource
Overview of C++ Polymorphism
CMSC 341 C++ and OOP.
COP 3330 Object-oriented Programming in C++
Classes C++ representation of an object
Object-Oriented Programming (Part 2)
CMSC 341 C++ and OOP.
Final Exam Review Inheritance Template Functions and Classes
CMSC 341 C++ and OOP.
Templates CMSC 202, Version 4/02.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Chapter 2 Objects and Classes Saurav Karmakar Spring 2007

2.1 What is OO programming? Object: an atomic unit that has structure and state Information hiding: Black-box analogy Encapsulation: grouping of data and functions Inheritance: mechanism allows extending functionality of an object.

How does C++ support OO Template: the logic is independent of the type Inheritance Polymorphism: can hold different types of objects. When operations are applied to the polymorphic type, the operation appropriate to the actual stored type is automatically selected.

2.2 Basic Class Syntax Class members: either data or functions and categorized into either public, protected,or private. Public: visible to all routines and may be accessed by any methods in any class. Private: not visible to nonclass routines and are accessed only by methods in its class. Protected: similar to private but visible to derived classes. Default: all members are private

Constructors Member functions/methods that describe how an object is declared and initialized. If no constructor defined, compilers will generate one called default constructor. Explicit constructors prevent automatic type conversion.

Constant Member Function Mutators change the state of the object but accessors are those who don’t. Constant member function: functions that do not change any class data member. const is a part of the function signature. [const] return_type name([const] parameter_list) [const];

Interface & Implementation Interface lists the class and its members and describes what can be done with the object. Implementation represents the internal process by which the interface specifications are met. Scope operator use : Systax : Classname ::member Signatures must match exactly.

TestIntCell.cpp #include <iostream.h> #include "IntCell.h" int main( ) { IntCell m; // Or, IntCell m( 0 ); but not IntCell m( ); m.write( 5 ); cout << "Cell contents: " << m.read( ) << endl; return 0; }

Big three: Destructor, Copy Constructor, and Operator = •Destructor tells how an object is destroyed and frees the allocated resources when it exists scope. ~IntCell(); •Copy Constructor allows a new object construction using the data in from existing one. IntCell a = b; // a new IntCellcall a IntCell a( b ); // a new IntCellcall a •Operator = copy assignment, copies data members using = by default. => may cause shallow copying.

This (predefine pointer)

2.3 Additional C++ Features Operator overloading: extending the types to which an operator can be applied. example: string x=“Mary’s score is:”; int y=95; string z=x+y; “.”, “.*”, “?:”, “sizeof” can’t be overloaded

Additional C++ Features Type conversion creates a temporary object of a new type– Example: int a = 5; double b = a; //implicit cast

2.5 Exceptions (report error) An object that stores information transmitted outside the normal return sequence and is used to signal exceptional occurrences. Handle exceptions by throw and catch clauses.

Exceptions example try { for (int n=0; n<10; n++) if (n>9) throw "Out of range"; } } catch (char * str) cout<< "Exception: " << str<< endl;

2.6 String Class C string: array of character terminated by ‘\0’ C++ standard string: a STL class with all overload operators and built-in functions http://www.bgsu.edu/departments/compsci/docs/string.html

Array size extension int *original =arr; arr = new int [12]; for(i=0; i<10; i++) arr[i] =original[i]; delete [] original;

Summary Construction/ destruction of objects Copy semantics Overloading Implicit/explicit type conversion Information hiding/atomicity