Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
Operator Overloading Fundamentals
Advertisements

Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Classes: A Deeper Look Systems Programming.
Chapter 11: Classes and Data Abstraction
Chapter 15: Operator Overloading
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
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.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Classes C++ representation of an object
Chapter 13: Overloading and Templates
Chapter 14: More About Classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Operator Overloading; String and Array Objects
Abstract Data Types Programmer-created data types that specify
Andy Wang Object Oriented Programming in C++ COP 3330
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Compilation and Debugging
Compilation and Debugging
Object Oriented Programming COP3330 / CGS5409
Andy Wang Object Oriented Programming in C++ COP 3330
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Chapter 14: More About Classes.
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Overview of C++ Overloading
Operator Overloading.
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Classes C++ representation of an object
C++ Class Members Class Definition class Name { public: constructor(s)
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Andy Wang Object Oriented Programming in C++ COP 3330 Test 1 Review Andy Wang Object Oriented Programming in C++ COP 3330

Exam Format Multiple choice True/false Determine whether programming statements are legal Declare and define member functions Coverage: up to aggregation and composition

Class Basics Class Object A user defined type A blueprint for building objects Object An instance of a class With a name, member data, and member functions

DDU Design Model Declare (class declaration in the .h file) Define (class members defined in the .cpp file) Use (usage of class by building objects)

class Declaration vs. Definition Similar in many aspects Return types, member function names, number of parameters and types, keyword const Exceptions Keyword friend in declaration only A member functions’ parameter names can differ in its declaration and definition Need scope resolution operator :: in definitions of member functions Default parameter values are set in declaration The initialization list is only used in definitions

To Call Member Functions of an Object Use the dot operator

Public vs. Private Can everything be public? Who can access the private member data? Member methods Objects of the same class

Constructors Have no return type Have the same name as the class Cannot be called directly as a member function Called automatically Default constructors Have no parameters Used when no other constructors exist

Conversion Constructor A constructor that can be called with one parameter Even with additional optional parameters Converts the parameter type to the object type Unless with the keyword explicit, conversion constructors are called automatically

Compilation Stages Compile stage Link stage Checks syntax and types Matches member function calls with their declarations in the header files Matches function calls to prototypes Reports undeclared variables Translates to .o files Link stage Matches function calls with their definitions Checks for missing or duplicate definitions Usually results in an executable

g++ Commands g++ -c frac.cpp g++ -o frac frac.o main.o Compiles frac.cpp to frac.o g++ -o frac frac.o main.o Links frac.o and main.o to build a frac binary program

Misc Don’t include .cpp files #include header files to satisfy “declare before use” Debugging Compilation errors (syntax with file and line numbers…) Linker errors (undefined or multiply defined functions) Runtime errors: fatal (crashes) and logical (bad results)

friend function Neither public nor private Not a member function Definition has no scope resolution operator Can access the private member data via object.private_member_data

Keyword const Can only be used for member functions of a class Not for friend functions Commonly used for accessor and display functions Can disallow changes when passed by reference Function(const int &a); Can disallow member data of the calling object to be modified during a call Function() const;

L-value vs. R-value L-value = r-value x = 10 vs. 10 = x? L-value can appear on the left-hand side of an assignment Only an L-value can be passed by reference (without const) R-value can appear on the right-hand side of an assignment

More on const const objects const member data static const member data Can only call const member functions Or, it won’t compile const member data Cannot be initialized in class declaration Need to use an initialization list static const member data Can be initialized in class declaration Cannot be changed afterwards A single instance shared by all objects of the same class

Destructors Have no return type Have same name as the class, with ~ in front Only one destructor per class, provided by default Does not take parameters Called automatically when an object goes out of scope

Operator Overloading Cannot change a number of properties Precedence a + b*c Associativity (a + b) + c = a + (b + c) Number of operands Cannot create new operators Can only overload existing ones Cannot change the operator meaning on the built-in types Can mix and match types (Fraction object + int object)

friend vs. Member Functions for Overloading friend functions Each takes two parameters for binary operators, one parameter for unary operators Needed for overloading the following operators +, -, *, /, ==, !=, >=, <=, >, <, <<, >>, etc. Otherwise, the first parameter can be a built-in type, which may not have the conversion constructor for the newly defined type Member functions Each takes one parameter for binary operators, no parameter for unary operators

Exceptions Postfix increment and decrement operators (++, --) Need a dummy parameter of type int

Aggregation Embed an object of one class type as member data of another class type Composition Embedded objects would typically not exist independent of the container object “Has a” relationship Need to use the initialization list to initialize the embedded objects