CIS 190: C/C++ Programming. Classes in C++

Similar documents
Lecture 7. Log into Linux New documents posted to course webpage

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Ch 6. Structures and Classes

Introduction to Classes

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Separate Compilation Model

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

PIC 10A Objects/Classes

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Programming 2. Object Oriented Programming. Daniel POP

Implementing an ADT with a Class

Introduction Of Classes ( OOPS )

Short Notes of CS201

Operator overloading: extra examples

Fast Introduction to Object Oriented Programming and C++

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

CS201 - Introduction to Programming Glossary By

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Page 1 of 5. *** person.cpp *** #include "person.h" #include <string> #include <iostream> using namespace std; *** person.h ***

Classes and Objects. Class scope: - private members are only accessible by the class methods.

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

Abstraction in Software Development

CSE 333 Midterm Exam 5/9/14 Sample Solution

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Abstract Data Types. Different Views of Data:

C++ Constructor Insanity

Object-Oriented Programming

G52CPP C++ Programming Lecture 9

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Homework 3. Due: Feb 25, 23:59pm. Section 1 (20 pts, 2 pts each) Multiple Choice Questions

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Financial computing with C++

III. Classes (Chap. 3)

CSC 172 Intermediate Progamming

ADTs & Classes. An introduction

SFU CMPT Topic: Classes

Intermediate Programming, Spring 2017*

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CS 247: Software Engineering Principles. ADT Design

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Programming & Abstraction

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

CSE 303, Autumn 2006, Final Examination 12 December 2006

CS24 Week 3 Lecture 1

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

CSC-140 Assignment 6

Functions, Arrays & Structs

CSCE 110 PROGRAMMING FUNDAMENTALS

CS349/SE382 A1 C Programming Tutorial

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Fundamentals of Programming Session 24

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Friend Functions and Friend Classes

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Dot and Scope Resolution Operator

PIC 10A. Lecture 17: Classes III, overloading

Object Oriented Design

Lecture 10: building large projects, beginning C++, C++ and structs

COMP6771 Advanced C++ Programming

Object Oriented Design

Constructors for classes

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Arrays. What if you have a 1000 line file? Arrays

Array Elements as Function Parameters

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Exercise 1.1 Hello world

Due Date: See Blackboard

Programming in C/C Lecture 3

Today s lecture. CS 314 fall 01 C++ 1, page 1

Object-Oriented Programming

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Computer Science II Lecture 1 Introduction and Background

CMSC 202 Midterm Exam 1 Fall 2015

6. Pointers, Structs, and Arrays. 1. Juli 2011

a data type is Types

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CMSC202 Computer Science II for Majors

Instantiation of Template class

Operator overloading

Software Design and Analysis for Engineers

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Structures. Lecture 5. Structures

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

Scope. Scope is such an important thing that we ll review what we know about scope now:

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Programming Assignment #2

C++ Important Questions with Answers

Transcription:

CIS 190: C/C++ Programming Classes in C++

Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors

Headers in C++ done same way as in C including user.h files: #include userfile.h including C++ libraries #include <iostream>

An example typedef struct bar{ int a; } BAR; bar.h #include bar.h #include bar.h #include foo.h int main() { BAR i; FOO j; typedef struct foo{ BAR x; char y; } FOO; foo.h } /*... */ return 0; main.c

An example typedef struct bar{ int a; } BAR; bar.h #include bar.h #include bar.h #include foo.h int main() { BAR i; FOO j; when we try to compile this typedef struct foo{ BAR x; char y; } FOO; foo.h } /*... */ return 0; main.c

An example typedef struct bar{ int a; } BAR; #include bar.h #include foo.h int main() bar.h { BAR i; In #include file included bar.h from foo.h:1:0, FOO j; from main.c:2: bar.h:1:16: typedef struct error: foo{ redefinition /*... */ of 'struct bar' In BAR file x; included from main.c:1:0: bar.h:1:16: char y; note: originally return defined 0; here In } FOO; file included from foo.h:1:0, } from main.c:2: bar.h:3:3: foo.h error: conflicting main.c types for 'BAR' when we try to compile this In file included from main.c:1:0: bar.h:3:3: note: previous declaration of 'BAR' was here

What the compiler is seeing typedef struct bar{ int a; } BAR; typedef struct bar{ int a; } BAR; #include bar.h bar.h typedef struct bar{ int a; } BAR; typedef struct foo{ BAR x; char y; } FOO; foo.h #include bar.h typedef struct bar{ int a; } BAR; typedef struct foo{ BAR x; char y; } FOO; int main() { BAR i; FOO j; /*... */ return 0; } main.c #include foo.h

What the compiler is seeing typedef struct bar{ int a; } BAR; typedef struct bar{ int a; } BAR; #include bar.h bar.h typedef struct bar{ int a; } BAR; typedef struct foo{ BAR x; char y; } FOO; foo.h #include bar.h typedef struct bar{ int a; } BAR; typedef struct foo{ BAR x; char y; } FOO; int main() { BAR i; FOO j; /*... */ return 0; } main.c #include foo.h

Header Protection we want to have the definition of the BAR struct in both: foo.h main.c easiest way to solve this problem is through the use of header guards

Header Guards in each.h file, use the following: #ifndef BAR_H #define BAR_H if not (previously) defined then define [CONTENTS OF.H FILE GO HERE] #endif /* BAR_H */ stop the if at this point (end of the file)

A fixed example #include bar.h #include foo.h typedef struct bar{ int a; } BAR; bar.h #include bar.h typedef struct foo{ BAR x; char y; } FOO; foo.h int main() { BAR i; FOO j; } /*... */ return 0; main.c

A fixed example #ifndef BAR_H #define BAR_H typedef struct bar{ int a; } BAR; #endif /*BAR_H*/ bar.h #ifndef FOO_H #define FOO_H #include bar.h typedef struct foo{ BAR x; char y; } FOO; #endif /*FOO_H*/ foo.h #include bar.h #include foo.h int main() { BAR i; FOO j; } /*... */ return 0; main.c

Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors

Functions in C++ very similar to functions in C variable scope remains the same can still pass things by value, or by reference implicit (arrays) or explicit (pointers) a few differences from functions in C no need to pass array length (just use empty brackets) void PrintArray (int arr []);

Using const in C++ functions when used on pass-by-value int SquareNum (int x) { } return (x * x); int SquareNum (const int x) { } return (x * x);

Using const in C++ functions when used on pass-by-value no real difference; kind of pointless changes to pass-by-value variables don t last beyond the scope of the function conventionally: not wrong, but not done

Using const in C++ functions when used on pass-by-reference void SquareNum (int *x) { } (*x) = (*x) * (*x); /* fine */ void SquareNum (const int *x) { } (*x) = (*x) * (*x); /* error */

Using const in C++ functions when you compile the const version: void SquareNum (const int *x) { } (*x) = (*x) * (*x); /* error */ error: assignment of read-only location '*x'

Using const in C++ functions when used on pass-by-reference huge difference prevents changes to variables, even when they are passed in by reference conventionally: use for user-defined types (structs, etc.) but don t use for simple built-in types (int, double, char) except maybe arrays

Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors

Procedural Programming up until now, everything we ve been doing has been procedural programming code is divided into multiple procedures procedures operate on data (structures), when given correct number and type of arguments examples: PrintTrain(), ReadSingerFile(), DestroyList(), ProcessEvents(), etc.

Object-Oriented Programming now that we re using C++, we can start taking advantage of object-oriented programming code and data are combined into a single entity called a class each instance of a given class is an object of that class type OOP is more modular, and more transparent

Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors

Example: Date Struct implementing a date structure in C: typedef struct date { int month; int day; int year; } DATE;

Example: Date Class implementing a date class in C++: class Date { public: int m_month; int m_day; int m_year; };

Functions in Classes let s add a function to the class that will print out the name of the month, given the number class Date { public: }; void OutputMonth(); int m_month; int m_day; int m_year;

OutputMonth void OutputMonth(); nothing is passed in to the function because it only needs to look at the m_month variable which is a member variable of the Date class just like OutputMonth()

OutputMonth void Date::OutputMonth() { switch (m_month) { case 1: cout << January ; break; case 2: cout << February ; break; case 3: cout << March ; break; case 4: cout << April ; break; /* etc */ case 11: cout << November ; break; case 12: cout << December ; break; default: cout << Error in Date::OutputMonth() ; } }

OutputMonth void Date::OutputMonth() { switch (m_month) { case 1: cout << January ; break; case 2: cout << February ; break; case 3: cout << March ; break; case 4: cout << April ; break; /* etc */ case 11: cout << November ; break; case 12: cout << December ; break; default: cout << Error in Date::OutputMonth() ; } } include class name; more than one class can have a function with the same name

OutputMonth void Date::OutputMonth() { this switch double (m_month) colon is called { case 1: cout << January ; break; the scope resolution case 2: cout << February ; break; operator, case and 3: associates cout << March ; break; the member case 4: function cout << April ; break; OutputMonth() /* etc */ with the class case Date 11: cout << November ; break; case 12: cout << December ; break; default: cout << Error in Date::OutputMonth() ; } }

OutputMonth void Date::OutputMonth() { switch (m_month) { case 1: cout << January ; break; we can directly access m_month case 2: cout << February ; break; because case 3: it is cout a member << variable March ; of break; the case Date 4: class, cout to which << April ; the break; OutputMonth() /* etc */ function belongs case 11: cout << November ; break; case 12: cout << December ; break; default: cout << Error in Date::OutputMonth() ; } }

Using the Date class Date today, birthday; cout << Please enter dates as DD MM YYYY << endl; // get today s date cout << Please enter today s date: ; cin >> today.m_day >> today.m_month >> today.m_year; // get user s birthday cout << Please enter your birthday: ; cin >> birthday.m_day >> birthday.m_month >> birthday.m_year; //echo output cout << Today s date is << today.outputmonth() << today.m_day <<, << today.m_year << endl; cout << Your birthday is << birthday.outputmonth() << birthday.m_day <<, << birthday.m_year << endl;

Using the Date class Date today, birthday; variables today and birthday are instances of the class Date cout << Please enter dates as DD MM YYYY << endl; // get today s date cout << Please enter today s date: ; cin >> today.m_day >> today.m_month >> today.m_year; they are both objects of type Date // get user s birthday cout << Please enter your birthday: ; cin >> birthday.m_day >> birthday.m_month >> birthday.m_year; //echo output cout << Today s date is << today.outputmonth() << today.m_day <<, << today.m_year << endl; cout << Your birthday is << birthday.outputmonth() << birthday.m_day <<, << birthday.m_year << endl;

Using the Date class Date today, birthday; cout << Please enter dates as DD MM YYYY << endl; // get today s date cout << Please enter today s date: ; cin >> today.m_day >> today.m_month >> today.m_year; when we are not inside the class (as we were in the OutputMonth() function) we must use the dot operator to access today s member variables // get user s birthday cout << Please enter your birthday: ; cin >> birthday.m_day >> birthday.m_month >> birthday.m_year; //echo output cout << Today s date is << today.outputmonth() << today.m_day <<, << today.m_year << endl; cout << Your birthday is << birthday.outputmonth() << birthday.m_day <<, << birthday.m_year << endl;

Using the Date class Date today, birthday; cout << Please enter dates as DD MM YYYY << endl; // get today s date cout << Please enter today s date: ; we also use the dot operator to cin >> today.m_day >> today.m_month >> today.m_year; call the member function OutputMonth() on the Date object today; again, note that we do not need to pass in the member variable m_month // get user s birthday cout << Please enter your birthday: ; cin >> birthday.m_day >> birthday.m_month >> birthday.m_year; //echo output cout << Today s date is << today.outputmonth() << today.m_day <<, << today.m_year << endl; cout << Your birthday is << birthday.outputmonth() << birthday.m_day <<, << birthday.m_year << endl;

Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors

Public, Private, Protected in our definition of the Date class, everything was public this is not good practice! we have three different options for access specifiers, each with their own role: public private protected

Example: Public, Private, Protected class Date { public: int m_month; private: int m_day; protected: int m_year; };

Using Public, Private, Protected public anything that has access to the birthday object also has access to birthday.m_month, etc. private m_day can only be accessed by member functions of the Date class; cannot be accessed in main(), etc. protected m_year can by accessed by member functions of the Date class and by member functions of any derived classes (we ll cover this later)

Access specifiers for Date class class Date { public: void OutputMonth(); private: int m_month; int m_day; int m_year; };

New member functions now that m_month, m_day, and m_year are private, how do we give them values, or retrieve those values? write public member functions to provide indirect, controlled access for the user

New member functions accessor functions: allow retrieval of private data members GetMonth(), GetDay(), GetYear() mutator functions: allow changing the value of a private data member SetMonth(), SetDay(), SetYear() service functions: provide support for the operations OutputMonth()

Access specifiers for Date class class Date { public: void OutputMonth(); int GetMonth(); int GetDay(); int GetYear(); void SetMonth(int m); void SetDay (int d); void SetYear (int y); private: int m_month; int m_day; int m_year; };

Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors

Constructors special member functions used to create (or construct ) new objects automatically called when an object is created initializes the values of all data members

Date class Constructors class Date { public: void OutputMonth(); Date (int m, int d, int y); private: int m_month; int m_day; int m_year; };

Date class Constructors class Date { public: void OutputMonth(); Date (int m, int d, int y); private: exact same name as the class int m_month; int m_day; int m_year; };

Date class Constructors class Date { public: void OutputMonth(); Date (int m, int d, int y); no return type, not even void private: int m_month; int m_day; int m_year; };

Constructor Definition Date::Date (int m, int d, int y) { m_month = m; m_day = d; m_year = y; }

Constructor Definition by using classes with private members and public functions, we can control almost everything can prevent incorrect values from being accepted by the constructor

Constructor Definition Date::Date (int m, int d, int y) { if (m > 0 && m <= 12) { m_month = m; } else { m_month = 1; } if (d > 0 && d <= 31) { m_day = d; } else { m_day = 1; } if (y > 0 && y <= 2100) { m_year = y; } else { m_year = 1; } }

Overloading we can define multiple versions of the constructor we can overload the function different constructors for: when all values are known when no values are known when some subset of values are known

All Known Values have the constructor set user-supplied values Date::Date (int m, int d, int y) { m_month = m; m_day = d; m_year = y; }

All Known Values have the constructor set user-supplied values Date::Date (int m, int d, int y) { } m_month = m; m_day = d; m_year = y; invoked when constructor is called with all arguments

No Known Values have the constructor set all default values Date::Date () { m_month = 1; m_day = 1; m_year = 1 }

No Known Values have the constructor set all default values Date::Date () { } m_month = 1; m_day = 1; m_year = 1 invoked when constructor is called with no arguments

Some Known Values have the constructor set some default values Date::Date (int m, int d) { m_month = m; m_day = d; m_year = 1 }

Some Known Values have the constructor set some default values Date::Date (int m, int d) { } m_month = m; m_day = d; m_year = 1 invoked when constructor is called with some arguments

Overloaded Date Constructor so far we have the following constructors: Date::Date (int m, int d, int y); Date::Date (int m, int d); Date::Date (); would the following be a valid constructor? Date::Date (int m, int y);

Avoiding Multiple Constructors defining multiple constructors for different known values is a lot of code duplication we can avoid this by setting default parameters in our constructors

Default Parameters in the function prototype only, provide default values you want the constructor to use Date (int m = 3, int d = 6, int y = 2014);

Default Parameters in the function definition literally nothing changes Date::Date (int m, int d, int y) { m_month = m; m_day = d; m_year = y; }

Using Default Parameters the following are all valid declarations: Date graduation(5,19,2014); Date today; Date halloween(10,25); Date july(4); // graduation: 5/19/2014 // today: 3/6/2014 // halloween: 10/25/2014 // july: 4/6/2014

Using Default Parameters the following are all valid declarations: Date graduation(5,19,2014); Date today; Date halloween(10,25); Date july(4); // graduation: 5/19/2014 // today: 3/6/2014 // halloween: 10/25/2014 // july: 4/6/2014

Using Default Parameters the following are all valid declarations: Date graduation(5,19,2014); Date today; NOTE: when you Date halloween(10,25); call a constructor Date july(4); with no arguments, you do not give it // graduation: empty 5/19/2014 parentheses // today: 3/6/2014 // halloween: 10/25/2014 // july: 4/6/2014

Default Constructors default constructor is provided by compiler will handle declarations of Date instances but, if you create any other constructor, the compiler doesn t provide a default constructor so make sure you always create a default constructor too, even if its body is just empty