CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

C++ Classes & Data Abstraction
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.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
CMSC 202 Lesson 7 Classes I. Warmup  Add the correct parameter list and return type to the following function: (hint: all the information you need is.
Object Oriented Programming (OOP) Lecture No. 11.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Objects and Classes Project 2 description.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
CMSC202 Computer Science II for Majors Lecture 05 – References Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Introduction to Object-oriented Programming
Pointer to an Object Can define a pointer to an object:
Structures and Classes
Procedural and Object-Oriented Programming
Classes and OOP.
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Review: Two Programming Paradigms
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Introduction to Object-oriented Programming
CS1201: Programming Language 2
This technique is Called “Divide and Conquer”.
Introduction to Object-oriented Program Design
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-oriented Design in Processing
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Inheritance Dr. Bhargavi Goswami Department of Computer Science
CMSC 202 Lesson 7 Classes I.
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Object-oriented Design in Processing
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
CS1201: Programming Language 2
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Object-oriented Design in Processing
CPS120: Introduction to Computer Science
CMSC 202 Lesson 8 Classes II.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 8 Object Oriented Programming (OOP)
Object-oriented Design in Processing
Presentation transcript:

CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC

Last Class We Covered Pointers – Review – Passing to Functions Using pointers to pass arrays to functions – Including C-Strings References – Creating – Passing to Functions 2

Any Questions from Last Time?

Today’s Objectives To understand the purpose and benefits of Object Oriented Programming To learn about classes in C++ – Class Methods (Functions) 4

Programming and Abstraction All programming languages provide some form of abstraction – Also called “information hiding” – Separates code use from code implementation Procedural Programming – Data Abstraction: using data structures – Control Abstraction: using functions Object Oriented Programming – Data and Control Abstraction: using classes 5

Procedural vs OOP Examples Procedural – Calculate the area of a circle given the specified radius – Sort this class list given an array of students – Calculate the student’s GPA given a list of courses 6 Object Oriented – Circle, you know your radius, what is your area? – Class list, sort your students – Transcript, what is this student’s GPA?

What is a Class? According to the dictionary: – A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common According to OOP principles: – A group of objects with similar properties, common behavior, common relationships with other objects, and common semantics 7

Blueprints Classes are “blueprints” for creating objects – A dog class to create dog objects – A car class to create car objects – A shoe class to create shoe objects The blueprint defines – The class’s state/attributes As variables – The class’s behaviors As methods 8

Objects Each instance of a class is also called an object of that class type You can create as many instances of a class as you want – Just like a “regular” data type, like int or float – There is more than one dog, or car, or shoe 9

Encapsulation Encapsulation is a form of information hiding and abstraction Data and functions that act on that data are located in the same place (inside a class) Goal: – Separate interface from implementation so that someone can use the code without any knowledge of how it works 10

Class Declaration Example class Car { public: bool AddGas(float gallons); float GetMileage(); // other operations private: float m_currGallons; float m_currMileage; // other data }; Methods Data Class Name Protection Mechanism

Class Rules – Coding Standard Class names – Always begin with capital letter – Use mixed case for phrases – General word for class (type) of objects Ex: Car, Boat, Building, DVD, List, Customer, BoxOfDVDs, … Class data (member variables) – Always begin with m_ Ex: m_fuel, m_title, m_name, … Class operations/methods – Always begin with capital letter Ex: AddGas(), Accelerate(), ModifyTitle(), RemoveDVD(), …

Methods and Member Variables Classes encapsulate both data and functions – Class definitions must contain both Member variables are the data of a class – Its attributes, or characteristics – e.g., breed of Dog, size of Shoe, make of Car Class methods are used to act on that data – e.g., Play() with Dog, Inspect() a Car 13

Example of Using a Class // Represents a Day of the Year class DayOfYear { public: void Output(); int m_month; int m_day; }; // Output method – displays a DayOfYear void DayOfYear::Output() { cout << m_month << "/" << m_day; } // Code from main() DayOfYear july4th; july4th.m_month = 7; july4th.m_day = 4; july4th.Output();

Method Implementation void DayOfYear::Output() { cout << m_month << "/" << m_day; } Class Name Scope Resolution Operator: indicates which class this method is from Method Name Method Body

Separating Classes into Files // Represents a Day of the Year class DayOfYear { public: void Output(); int m_month; int m_day; }; // Output method – displays a DayOfYear void DayOfYear::Output() { cout << m_month << “/” << m_day; } Class Definition Goes in file ClassName.cpp (DayOfYear.cpp) Class Declaration Goes in file ClassName.h (DayOfYear.h)

Using Classes // code from main() DayOfYear july4th; july4th.m_month = 7; july4th.m_day = 4; july4th.Output(); Dot Operator Object Name (Variable) Class Methods and Members Constructor (we’ll cover this soon)

Dot and Scope Resolution Operator Used to specify "of what thing" they are members Dot operator: – Specifies member of particular object Class method or member variable Scope resolution operator: – Specifies what class the function’s definition belongs to

More about Encapsulation Class methods do not need to be passed information about that class object – Notice that the Output() method does not have any parameters Class methods are called on a class object – They know everything about that object already Remember, classes contain code and data! 19

Time for… 20

Livecoding Exercise Create a Rectangle class with – Member variables for height and width – Class methods to: Calculate area Calculate perimeter Check if it’s square “Rotate” the rectangle Create both Rectangle.h and Rectangle.cpp 21

Announcements Project 1 has been released Found on Professor’s Marron website Due by 9:00 PM on February 23rd Get started on it now! Make sure to read and follow the coding standards for this course! Next time: more on Classes and Objects 22