Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.

Slides:



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

Based on Java Software Development, 5th Ed. By Lewis &Loftus
CS 211 Inheritance AAA.
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.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lecture 9 Concepts of Programming Languages
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
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 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
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.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Classes, Interfaces and Packages
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.
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.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Written by: Dr. JJ Shepherd
Haidong Xue Summer 2011, at GSU
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
OOP What is problem? Solution? OOP
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
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?
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object-Oriented Programming Using C++
Lecture 9 Concepts of Programming Languages
CS212: Object Oriented Analysis and Design
Corresponds with Chapter 7
Object-Oriented Programming
Learning Objectives Classes Constructors Principles of OOP
Object-Oriented Programming
Defining Classes and Methods
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
CIS 199 Final Review.
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B

Overview Finish Sorting recap Thinking object oriented recap Classes in C++ Building a class in C++ (real time demo)

Sorting recap Bubble sort Insertion sort Selection sort Merge sort Heapsort Quicksort

Thinking object oriented recap Language as an influence of thought process OO concepts – Separation of interface and implementation – Information hiding – Inheritance Writing reusable code

Exciting Note for Today The gcc compiler now requires C++ to build – Essentially means parts of the gcc compiler are written in C++ =2b15d2ba7eb3a25dfb15a7300f4ee7a141ee8 539

Structures Structures provide a way to organize data Structures in C++ are essentially classes, not true in C

Classes An object is a variable that has member functions (instance methods) A class is a data type whose variables are objects Class – Describe the kind of values the variables hold (state) – Describe the member functions (behavior)

Terminology The book uses member to mean a particular instance of a class The book uses members to mean attributes of a class (variables and methods) Function and method are somewhat used interchangeably Similar: – member variable = instance variable – member method = instance method

Classes Provide encapsulation – Combining a number of items, such as variables and functions, into a single package, such as an object of some class (or instance of the class)

Scope Resolution Operator ClassName::method_name Used to identify the scope, class in this case, that the method belongs to as there may be more than 1 instance of method_name Scope resolution isn’t necessary if you are also a member of that class

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

Abstract Data Types A formal specification of the separation of implementation and interface Developer can use ADTs without concern for their implementation Using classes, you can define your own ADTs – This allows for reusable code

Tips for writing ADTs Make all the member variables private attributes of the class Provide a well defined public interface to the class and don’t change it Make all helper functions private

Intro to Inheritance in C++ Derived (aka child or sub) classes take on (inherit) the attributes of the parent (aka base or super) class class Timestamp : public Date { … };

For Lab2 Read “The const Parameter Modifier” section – Page 620 in the textbook int Date::days_until(const Date& other) const{…} const for parameters – Means the method cannot modify the parameter const at the end of the function declaration – Means that the method cannot not modify its own instance’s state

For Monday Read chapter 11 in the C++ book – Again, think about OO design themes in the C++ context The textbook is available in the library

Building a class demo