aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

Similar documents
Today. cis15-spring2010-parsons-lectii.1 2

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

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

OBJECT ORIENTED PROGRAMMING USING C++

COMP 2355 Introduction to Systems Programming

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:

Object Oriented Design

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:

a data type is Types

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

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

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

CPSC 427: Object-Oriented Programming

CS3157: Advanced Programming. Outline

OBJECT ORIENTED PROGRAMMING

Array Elements as Function Parameters

Fast Introduction to Object Oriented Programming and C++

Short Notes of CS201

Procedures, Parameters, Values and Variables. Steven R. Bagley

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

BITG 1113: POINTER LECTURE 12

CS201 - Introduction to Programming Glossary By

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

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

Initializing and Finalizing Objects

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CE221 Programming in C++ Part 1 Introduction

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

W3101: Programming Languages C++ Ramana Isukapalli

primitive arrays v. vectors (1)

PIC 10A Objects/Classes

AN OVERVIEW OF C++ 1

Object-Oriented Programming, Iouliia Skliarova

Special Topics: Programming Languages

ITP 342 Mobile App Dev. Fundamentals

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Introduction to Classes

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

G52CPP C++ Programming Lecture 13

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

C++ Important Questions with Answers

Pointers, Dynamic Data, and Reference Types

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Computer Programming

Tokens, Expressions and Control Structures

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

Object-Oriented Principles and Practice / C++

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Instantiation of Template class

C++ for Java Programmers

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

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

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

A brief introduction to C++

CS24 Week 3 Lecture 1

UEE1302 (1102) F10: Introduction to Computers and Programming

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

C++ For Science and Engineering Lecture 27

1d: tests knowing about bitwise fields and union/struct differences.

Object oriented programming with C++

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

QUIZ. What is wrong with this code that uses default arguments?

CMSC 202 Midterm Exam 1 Fall 2015

CS304 Object Oriented Programming Final Term

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

ADTs & Classes. An introduction

Input And Output of C++

AIMS Embedded Systems Programming MT 2017

The University of Nottingham

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

C++ Classes, Constructor & Object Oriented Programming

1B1b Classes in Java Part I

Unified Modeling Language a case study

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Memory and Pointers written by Cathy Saxton

CPSC 427a: Object-Oriented Programming

Absolute C++ Walter Savitch

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Common Misunderstandings from Exam 1 Material

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Introduction Of Classes ( OOPS )

Structured Data. CIS 15 : Spring 2007

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Variables. Data Types.

COMP 2355 Introduction to Systems Programming

Abstract Data Types. Different Views of Data:

CS 0449 Sample Midterm

Syntax and Variables

Object Reference and Memory Allocation. Questions:

Module 9. Templates & STL

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

Topic 10: Introduction to OO analysis and design

Transcription:

topics: objects and class design command line arguments resources: Pohl, chapter 4 cis15 advanced programming techniques, using c++ lecture # II.1 cis15-ozgelen-lecii.1 1 class and struct struct comes from C class is new in C++ aggregate data types both are aggregate types, meaning that they group together multiple fields of data ; don t forget to put a semi-colon at the end of the structure definition! in C, the tag (point) is optional and does not constitute a data type (you need to use typedef as well) but in C++, the tag is considered a data type, hence the above example is a data type definition which means that you can use point as a data type, e.g.: cis15-ozgelen-lecii.1 2 point p; the fields or elements of an aggregate data type are called members and they are referred to using dot notation, e.g.: p.x = 7.0; p.y = 10.3; you can also use a pointer to access members of an aggregate data type, e.g.: p->x = 12.3; but we will discuss pointers in the next unit, so don t worry about this now you can also declare a structure and variable in the same statement, e.g.: struct { mypoints[3] = { {1, 2, {3, 4, {5, 6 ; member functions in C++, members of aggregate data types can be functions (C only allows data members) in object-oriented programming (OOP) lingo, the word method is often used instead of function the reason to define functions inside an aggregate data type is to follow the OOP principle of encapsulation operations should be packaged with data void print() const { cis15-ozgelen-lecii.1 3 cis15-ozgelen-lecii.1 4

void set( double u, double v ) { x = u; y = v; ; // end of struct--don t forget semi-colon! point w; w.set( 1.2, 3.4 ); cout << "point = "; w.print(); notes: const keyword in definition of print method indicates that the data members will not be modified inside the method notice that the set method changes the values of the data members this is considered good OOP practise defining the methods inside the struct definition is called in-line declaration ; this is generally only okay for short, concise methods cis15-ozgelen-lecii.1 5 the class scope operator can be used when in-line declarations are inappropriate void set( double u, double v ); ; // end of struct--don t forget semi-colon! void point::print() const { // end of print() void point::set( double u, double v ) { x = u; y = v; // end of set() cis15-ozgelen-lecii.1 6 point w; w.set( 1.2, 3.4 ); cout << "point = "; w.print(); // end of main() public and private access members of structures can be public or private public means that any code can access the members private means that only code inside the class or struct can access the members (or friend classes, to be discussed later in the term) typically, following good OOP practice, all data members are private and only function members are public (but not all only those that need to be accessed outside of the struct or class) void set( double u, double v ); ; // end of struct--don t forget semi-colon! cis15-ozgelen-lecii.1 7 cis15-ozgelen-lecii.1 8

in C++, keyword class is introduced classes vs structs the difference between structs and classes is: in a struct, the members are public by default in a class, the members are private by default class point { void set( double u, double v ); ; // end of class--don t forget semi-colon! void point::print() const { // end of print() void point::set( double u, double v ) { x = u; y = v; // end of set() point w; w.set( 1.2, 3.4 ); cout << "point = "; w.print(); // end of main() otherwise, class and struct are syntactically the same but by convention, C++ programmers tend to use class cis15-ozgelen-lecii.1 9 cis15-ozgelen-lecii.1 10 class scope the class scope operator is two colons (::) the :: operator has the highest precedence in the language, so it always gets evaluated first there are two versions of the operator: binary and unary we already saw the binary version: point::print(), which is used to refer to a variable s class scope (also called local scope ) the unary version is like this: ::count and is used to refer to a variable s external scope (e.g., for a global variable) example from the book: int count = 0; // delcare global variable ++::count; // global count // end of how_many() this is only necessary since count is declared twice if you didn t have the ::count, then the second time, it would also refer to the local variable it is better practise not to use global variables; or at least if you do, give them unique names to avoid confusion :-) void how_many( double w[], double x, int& count ) { for ( int i=0; i<n; ++i ) { count += ( w[i] == x ); // local count cis15-ozgelen-lecii.1 11 cis15-ozgelen-lecii.1 12

nested classes this pointer classes can be nested example from the book: char c; // global scope class X { char c; // local scope in class X class Y { void foo( char e ) { X t; ::c = t.c = c = e; char c; // local scope in class Y ; ; the scope of the third (last) c is X::Y::c the keyword this is used to refer to an instance of a class from within itself it is a pointer something we will discuss at length in the next unit point inverse() { x = -x; y = -y; return (*this); this function returns a pointer to itself, i.e., the address of the object in memory we ll come back to this when we discuss pointers cis15-ozgelen-lecii.1 13 cis15-ozgelen-lecii.1 14 static members const members the static keyword is used to refer to members that do not need to be instantiated in other words, it is independent of any class variable class point { static int dimensions; ; point::dimensions = 2; // initialize but never instantiate point cis15-ozgelen-lecii.1 15 members with the const keyword in their definition cannot be modified this refers either to data members or to function members to indicate that the data members contained therein are not modified class point { const int dimensions ; ; void point::print() const { // end of print() note that the mutable keyword can override this cis15-ozgelen-lecii.1 16

mutable int delta; means that even if delta is referenced inside a const function, its value can be modified. Example: class person { void setname( string n ) { name = n; void setage( int a ) { age = a; void setssn( unsigned long ssn ) { soc_sec = ssn; void bday() const { ++age; void print() const { cout << name << " is " << age << " years old. SSN : " << soc_sec << endl; mutable int age; // always modifiable unsigned long soc_sec; string name; int main(){ const person ira; ira.setname("ira Pohl"); ira.setage(38); ira.setssn(1110111); ira.print(); ira.bday(); // ira.age is mutable ira.print(); cis15-ozgelen-lecii.1 17 cis15-ozgelen-lecii.1 18 special types of classes: containers there are several special types of classes in C++ the first we will discuss is called a container it is a class designed to hold large numbers of objects class ch_stack { void reset() { top = EMPTY; void push( char c ) { s[++top] = c; char pop() { return s[top--]; char top_of() const { return s[top]; bool empty() const { return( top==empty ); bool full() const { return( top==full ); cis15-ozgelen-lecii.1 19 ; enum{ max_len = 100, EMPTY = -1, FULL = max_len - 1 ; char s[max_len]; int top; ch_stack s; char str[40] = { "hello world!" ; int i = 0; cout << "str=" << str << endl; s.reset(); while( str[i] &&! s.full() ) { s.push( str[i++] ); cout << "reversed str="; while (! s.empty() ) { cout << s.pop(); cout << endl; // end of main() cis15-ozgelen-lecii.1 20

class design command-line arguments data members should be private ( hidden ) function members are often public (but not always private function members can be used for computations internal to a class) functions that do not modify data members should be const pointers add indirection (we ll talk about that later) a uniform set of functions should be included: set(), get(), print() UML (unified modeling language) provides a graphical method for representing classes point dimension x y print() set() inverse() example: int main( int argc, char **argv ) { cout << "argc = " << argc << endl; for ( int i=0; i<argc; i++ ) { cout << "[" << i << "]=" << argv[i] << endl; // end of main() executed from the unix command-line like this: unix$./a.out asdf 45 argc = 3 [0]=./a.out [1]=asdf [2]=45 cis15-ozgelen-lecii.1 21 cis15-ozgelen-lecii.1 22