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

ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Defining New Types Lecture 21 Hartmut Kaiser
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
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.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
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.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Motivation for Generic Programming in C++
C ++ MULTIPLE CHOICE QUESTION
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Classes C++ representation of an object
Class and Objects UNIT II.
Abstract Data Types and Encapsulation Concepts
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Programming with ANSI C ++
Object-Oriented Programming (OOP) Lecture No. 45
Chapter 1 C++ Basics Review
Review Bernard Chen Spring 2006.
Review: Two Programming Paradigms
University of Central Florida COP 3330 Object Oriented Programming
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 2 Objects and Classes
Lecture 9 Concepts of Programming Languages
Exceptions An exception signals an error, and has the ability to propagate upward through the call stack for easier management To “raise” an exception,
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Data Types and Encapsulation Concepts
CS212: Object Oriented Analysis and Design
Defining New Types Lecture 22 Hartmut Kaiser
Object-Oriented Programming (OOP) Lecture No. 22
Lists - I The List ADT.
Lists - I The List ADT.
Exceptions, Templates, and the Standard Template Library (STL)
Overview of C++ Polymorphism
Java Programming Language
CMSC 341 C++ and OOP.
Classes C++ representation of an object
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Final Exam Review Inheritance Template Functions and Classes
CMSC 341 C++ and OOP.
Templates CMSC 202, Version 4/02.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Chapter 2 Objects and Classes Bernard Chen Spring 2006

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: allows us to implement new types (classes) that share same logic

2.2 Basic Class Syntax Class members: either data or functions and categorized into either public, protected,or private. Public: visible to an instance of (object) a class Private: visible only inside an instance of a class Protected: similar to private but visible to derived classes. Default: all members are private

Constructors Member functions 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 Constant functions (accessors) : functions that do not change any data member. const is a part of the function signature. [const] return_type name([const] parameter_list) [const]; Interface: describes what can be done to the object, i.e. the header. Implementation: represents internal processes specified by the interface.

Big three: Destructor, Copy Constructor, and Operator = •Destructor tells how an object is destroyed and freesdresources when it exists scope. ~IntCell(); •Copy Constructor allows a new object construct using the data in an existing one. IntCella(5); // a new IntCellcall a IntCellb(a); // another IntCellcall b •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 (intn=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

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