csci 125 introduction to computer science and …...2020/07/04  · programming ii lecture 6: user...

22
09.07.20 19:32 CSCI 125 Introduction to Computer Science and Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2)

Upload: others

Post on 12-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

09.07.20 19:32CSCI 125 Introduction to Computer Science and

Programming II Lecture 6: User Defined Datatypes

Jetic Gū2020 Summer Semester (S2)

Page 2: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Bad

• I understand online studying can be bothering

• This is NOT an excuse to COPY others’ work

• It’s also a bad idea to ignore practice problems

P0 Midterm

Page 3: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Overview

• Focus: Basic C/C++ Syntax

• Architecture: Linux/Unix OS

• Core Ideas:

1. Enumeration and Typedef

2. namespace

Page 4: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Data Types• Primary √

• Integers, Characters, Boolean √

• Floating point √

• Void√

• Derived

• Function√, Array√, Pointer, Reference√

• User Defined

• Struct, Class, Enumerate, Typedef

Review

P0 Review

Page 5: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Enumeration and Typedef

Summary

P1 Enum. & Typedef

Important? No, I never used them, not even once

Page 6: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Enumeration

• Data type: a set of values together with a set of operations on those values

• To define a new simple data type, called enumeration type, we need three things:

• A name for the data type

• A set of values for the data type

• A set of operations on the values

Concep

t

P1 Enum. & Typedef

Page 7: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Enumeration• A new simple data type can be defined by specifying its name and the values,

but not the operations

• The values must be identifiers (usually all UPPERCASE letters)

• Syntax:

enum typeName {value1, value2, ...};

• value1, value2, … are identifiers called enumerators

• value1 < value2 < value3 <...

Concep

t

P1 Enum. & Typedef

Page 8: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Enumeration

• Enumeration type is an ordered set of values

• If a value has been used in one enumeration type it can’t be used by another in same scope

• The same rules apply to enumeration types declared outside of any blocks

Concep

t

P1 Enum. & Typedef

Page 9: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Enumeration

• Valid examples:

• Colours

enum colours {RED, GREEN, BLUE, YELLOW, BURGUNDY};

• Status

enum status {MARRIED, SINGLE, DIVORCED};

Concep

t

P1 Enum. & Typedef

Page 10: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Enumeration

• Invalid examples:

enum colours {ORANGE, RED, YELLOW, BURGUNDY};

enum fruits {ORANGE, APPLE, STRAWBERRY};

• Values here needs to be unique identifiers in that scope

Concep

t

P1 Enum. & Typedef

Page 11: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Declaring Variables

• Syntax, declaration just like any other type

dataType identifier1, identifier2, ...;

• e.g.

enum colours {RED, GREEN, BLUE, YELLOW, BURGUNDY} c; // declaring the type and variable c

colours border=RED, background=GREEN;

Concep

t

P1 Enum. & Typedef

Page 12: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Relational Operators

enum colours {RED, GREEN, BLUE, YELLOW, BURGUNDY};

• An enumeration type is an ordered set of values:

RED <= GREEN is true

GREEN < BLUE is true

BURGUNDY > BLUE is true

Concep

t

P1 Enum. & Typedef

Page 13: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Input /Output of Enumeration Types

• Enumeration type cannot be input/output directly

• Modifying cin and cout is quite complicated (not covered in CSCI125)

Concep

t

P1 Enum. & Typedef

Page 14: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Anonymous Data Types• Anonymous type

values are directly specified in the declaration, with no type name

enum {RED, GREEN, BLUE, YELLOW, BURGUNDY} c;

// c is a variable here

• Drawbacks:

• Cannot pass/return an anonymous type to/from a function

• Values used in one type can be used in another, but are treated differently:

enum {EN, DE, FR, CN} language1;

enum {EN, DE, FR, CN} language2;

language1 = language2; // doesn’t work

Concep

t

P1 Enum. & Typedef

Page 15: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

typedef Statement

• Creates a synonym / alias to a data type

• Syntax

typedef existingType newTypeName;

• typedef does not create any new data types

• (Almost) the same as preprocessor directive

Concep

t

P1 Enum. & Typedef

Page 16: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

typedef Statement

• Creates a synonym / alias to a data type

• typedef does not create any new data types

• (Almost) the same as preprocessor directive (but is not!)

• This will NOT compile

Concep

t

P1 Enum. & Typedef

#include <iostream>

#include <string>

using namespace std;

typedef int notInt;

notInt main() {

int a = 12;

cout << double(a) << endl;

return 0;

}

Page 17: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Namespace

Summary

P2 Namespace

Page 18: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Namespaces

• ANSI/ISO standard C++ was officially approved in July 1998

• Most of the recent compilers are also compatible with ANSI/ISO standard C++

• For the most part, standard C++ and ANSI/ISO standard C++ are the same

• However, ANSI/ISO Standard C++ has some features not available in Standard C++

Concep

t

P2 Namespace

Page 19: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Namespaces• Global identifiers in a header file used in a program become global in the

program

• Syntax error occurs if an identifier in a program has the same name as a global identifier in the header file

• Same problem can occur with third-party libraries

• Common solution: third-party vendors begin their global identifiers with _ (underscore)

• Do not begin identifiers in your program with _

Concep

t

P2 Namespace

Page 20: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Declaration• ANSI/ISO Standard C++ attempts to solve this problem with the namespace mechanism

• Syntax

namespace namespace_name {

members

}

• Member

• Variable declarations; Constant declarations

• functions; function prototypes

• another namespace(s)

Concep

t

P2 Namespace

Page 21: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Declaration

namespace csci125 {

int classSize;

int classID;

void printGrade();

}

• Then to use it

std::cout << csci125::classID << std::endl;

Concep

t

P2 Namespace

Page 22: CSCI 125 Introduction to Computer Science and …...2020/07/04  · Programming II Lecture 6: User Defined Datatypes Jetic Gū 2020 Summer Semester (S2) Bad • I understand online

Scope

• The scope of a namespace member is local to the namespace

• Ways a namespace member can be accessed outside the namespace:

csci125::classSize

using namespace csci125; // then you can access any member without csci125::

using csci125::classSize; // then you can just use classSize

Concep

t

P2 Namespace