More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 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.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
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
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
ECE122 Feb. 22, Any question on Vehicle sample code?
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
More C++ Bryce Boe 2013/10/23 CS24, Fall Outline Project 1 Review Alignment and Padding Finish C++ Introduction.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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?
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
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.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Creating Your Own 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?
Haidong Xue Summer 2011, at GSU
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
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?
Creating Your OwnClasses
Anatomy of a class Part I
An Introduction to Java – Part II
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
Constructors and Other Tools
CIS 199 Final Review.
Classes and Objects CGS3416 Spring 2019.
Lecture 8 Object Oriented Programming (OOP)
Anatomy of a class Part I
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Presentation transcript:

More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C

Outline Project 1 Review Finish C++ Introduction

PROJECT 1 REVIEW

Sample Solution – Solution will NOT be posted

C++ INTRODUCTION CONTINUED

Data Hiding Declaring member (instance) variables as private, why? – Assists in separation of implementation and interface – Allows for input validation and state consistency

Declaring Private attributes class Date { int day; // this section is private by default int month; // though you should be explicit public: void output_date(); private: int year; };

Accessor methods Sometimes called getters Instance methods that return some data to indicate the state of the instance Typically prefixed with get_ int Date::get_day() { return day; }

Mutator methods Sometimes called setters Instance methods that update or modify the state of the instance Typically prefixed with set_ void Date::set_day(int d) { day = d; }

Overloading Instance Methods Defining methods of a class with the same name, but different parameters void Date::update_date(int d, int m, int y) {…} void Date::update_date(Date &other) {…}

Class Constructors A constructor is used to initialize an object It must: – Have the same name as the class – Not return a value Constructors should be declared public – To ponder: what does it mean to have a non- public constructor? Always define a default constructor

Example class Date { public: Date(int d, int m, int y); Date(); // default constructor private: int day, month, year; };

Two ways to initialize variables From the constructor declaration (implementation) Method 1: Initialize in the constructor initialization section Date::Date() : day(0), month(0), year(0) {} Method 2: In the method body Date::Date() { day = 0; month = 0; year = 0; }

Example Constructor Usage Date a (10, 10, 11); // use the 3 param constructor Date b; // correct use of default constructor Date c(); // incorrect use of default constructor // This is actually a function definition Date d = Date(); // valid, but inefficient

Anonymous Instances An instance that is not bound to a variable Date d = Date(); In the above example there are actually two instances of class Date – The first is represented by d – The second is the anonymous instance represented by Date() The assignment operator is used to transfer information from the anonymous instance to d

Finish Example from Tuesday