Review What is an object? What is a class?

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Lecture 5: Interfaces.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 9 Classes: A Deeper Look, Part I Part II.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Matthew Small Introduction to Object-Oriented Programming.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Procedural and Object-Oriented Programming
Classes (Part 1) Lecture 3
Examples of Classes & Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Classes Object-oriented programming: Example: Bank transactions
Chapter 3: Using Methods, Classes, and Objects
Constructor & Destructor
Anatomy of a class Part II
Templates.
Lecture 4: Interface Design
Corresponds with Chapter 7
Object-Oriented Programming
Classes & Objects: Examples
Learning Objectives Classes Constructors Principles of OOP
Introduction to Classes and Objects
Object-Oriented Programming
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Method of Classes Chapter 7, page 155 Lecture /4/6.
CIS 199 Final Review.
CS410 – Software Engineering Lecture #5: C++ Basics III
Anatomy of a class Part II
Object Oriented programming
Constructors and Deconstructor
Lecture 8 Object Oriented Programming (OOP)
Constructors & Destructors
Object Oriented Programming (OOP) Lecture No. 12
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

Review What is an object? What is a class? What are the steps to creating a new object type and using it? What is the scope resolution operator?

Class interface: Protection Levels and Constructors

Protection Levels When we design a class we can control how member functions and data of that class can be accessed. Each member of the class is assigned a protection level: Public Members : Object data and functions that can be accessed from both outside and inside the member functions of the object. Private Members : Object data and functions that can be accessed only from within member functions of the object. Code that implements the class has accesses to private data, code that uses the class does not have access to private data. The primary purpose of protection levels is to allow programmers to provide an unchanging public interface for a class while being able to modify the way the class is implemented without breaking code that uses the class. This is a mechanism for information hiding.

sample1.cpp Program output: Decimal:0.5 Now lets uncomment line 33, Compiler output: sample1.cpp: In function ‘int main()’: sample1.cpp:9: error: ‘int Fraction::numer’ is private sample1.cpp:33: error: within this context

sample2.cpp Program output: With line 39 uncommented, Compiler output: Hello!!! Decimal:0.5 With line 39 uncommented, Compiler output: sample2.cpp:32: error: ‘void Fraction::PrintHello()’ is private sample2.cpp:39: error: within this context

More on Protection Levels Reasons for data hiding (private members): Makes interface simpler for user. Principle of least privileged (need-to-know) More secure. Less chance for misuse (accidental or malicious). Class implementation easy to change without affecting other modules that use it. Other things to note: If protection levels not given, members default to private. When designing a class we should make private any members not part of the class interface.

Constructors A constructor is a special member function in a class whose purpose is usually to initialize the members of an object. A constructor is easy to recognize because: It has the same name as the class. It has no return type. Circle Example:

Constructors continued A constructor is a function so you can write any code inside it you want. What is special about constructors? Called automatically whenever you instantiate an object (eg. Circle C1; ) The constructor function is not called explicitly (eg. C1.Circle()) The term default constructor refers to a constructor with no parameters. Every class must have a constructor, if you do not supply one an empty default constructor is created automatically. How do we pass arguments to constructors with parameters?

Passing Arguments to a Constructor It is simple, really. Just add (…) as part of the object instantiation: Circle C1(5); /* declares a new Circle object with the argument 5 passed to the second Circle constructor as parameter r */ Let's look at the Fraction example...