G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Similar documents
G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

G52CPP C++ Programming Lecture 12

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

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

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

G52CPP C++ Programming Lecture 20

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 303: Concepts and Tools for Software Development

The University of Nottingham

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Heap Arrays. Steven R. Bagley

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

And Even More and More C++ Fundamentals of Computer Science

Variables and literals

Fast Introduction to Object Oriented Programming and C++

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

C++ (Non for C Programmer) (BT307) 40 Hours

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

CS304 Object Oriented Programming Final Term

In Java we have the keyword null, which is the value of an uninitialized reference type

QUIZ Friends class Y;

Advanced Systems Programming

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

G52CPP C++ Programming Lecture 15

Principles of Object Oriented Programming. Lecture 4

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

10. Object-oriented Programming. 7. Juli 2011

Kurt Schmidt. October 30, 2018

G52CPP C++ Programming Lecture 16

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

An Introduction to C++

Heap Arrays and Linked Lists. Steven R. Bagley

G52PGP. Lecture OO2 Object Oriented programming in C

Software Design and Analysis for Engineers

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

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

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

Object-Oriented Programming

Chapter 4 Defining Classes I

Ch. 11: References & the Copy-Constructor. - continued -

G52CPP C++ Programming Lecture 17

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

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

CS61C Midterm Review on C & Memory Management

Short Notes of CS201

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

OBJECT ORIENTED PROGRAMMING

Common Misunderstandings from Exam 1 Material

Review of the C Programming Language for Principles of Operating Systems

STRUCTURING OF PROGRAM

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS201 - Introduction to Programming Glossary By

Object Oriented Software Design II

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

C++ Inheritance and Encapsulation

Ticket Machine Project(s)

What are the characteristics of Object Oriented programming language?

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

September 10,

CSE 374 Programming Concepts & Tools

Final CSE 131B Spring 2004

Object-Oriented Programming, Iouliia Skliarova

COMP 2355 Introduction to Systems Programming

Sums. Here's a (poor) program to add three numbers

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

COMP 2355 Introduction to Systems Programming

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Software Design and Analysis for Engineers

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Chapter 15: Object Oriented Programming

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

EECS168 Exam 3 Review

Object-Oriented Programming, Iouliia Skliarova

The University of Nottingham

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

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Review of the C Programming Language

Lecture 2, September 4

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Data Abstraction. Hwansoo Han

Java Object Oriented Design. CSC207 Fall 2014

CS61, Fall 2012 Section 2 Notes

CA341 - Comparative Programming Languages

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

EL2310 Scientific Programming

VARIABLES AND TYPES CITS1001

C++ Important Questions with Answers

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

CLASSES AND OBJECTS IN JAVA

Transcription:

G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1

This lecture classes (and C++ structs) Member functions inline functions 2

Last lecture: predict the sizes 3

#pragma pack(1) #include <cstdio> struct A { int i; char c; }; union B { int i; char c; }; #pragma pack(1) struct C { int i; char c; }; union D { int i; char c; }; Example: char : 1 int : 4 struct A :? union B :? struct C :? union D :? int main( int argc, char** argv ) { printf( "sizeof(char): %d\n", sizeof(char) ); printf( "sizeof(int): %d\n", sizeof(int) ); printf( "sizeof(struct A): %d\n", sizeof(struct A) ); printf( "sizeof(union B): %d\n", sizeof(union B) ); printf( "sizeof(struct C): %d\n", sizeof(struct C) ); printf( "sizeof(union D): %d\n", sizeof(union D) ); return 0; } 4

#pragma pack(1) #include <cstdio> struct A { int i; char c; }; union B { int i; char c; }; #pragma pack(1) struct C { int i; char c; }; union D { int i; char c; }; int main( int argc, char** argv ) { printf( "sizeof(char): %d\n", sizeof(char) ); printf( "sizeof(int): %d\n", sizeof(int) ); printf( "sizeof(struct A): %d\n", sizeof(a) ); printf( "sizeof(union B): %d\n", sizeof(b) ); printf( "sizeof(struct C): %d\n", sizeof(c) ); printf( "sizeof(union D): %d\n", sizeof(d) ); return 0; } Example: char : 1 int : 4 struct A : 8 union B : 4 struct C : 5 union D : 4 5

classes and structs 6

C-style structs Can still use structs in C++ Everything for structs so far applies to both C and C++ We will call them C-style structs These are usually called POD structs (Plain Old Data) If you use only C features: structs in C++ work as for C, i.e. you can predict sizeof(), can malloc() space for them, etc Everything we have seen so far is valid for POD structs Note: C++11 uses the terms trivially-copyable and standard-layout The restrictions are loosened compared with C++98/C++03 Look up these terms if you are interested, and see: http://en.cppreference.com/w/cpp/types/is_trivially_copyable http://en.cppreference.com/w/cpp/types/is_standard_layout 7

Structs in C++ vs C In C++ you can add functions to structs And inheritance, static members, etc, etc, etc If you use ANY C++ only features (e.g. add functions or use inheritance), their behaviour may change If you have used ANY C++ only features, DO NOT try to treat them as C structs you may get a nasty surprise e.g. size may grow or it may have hidden parts (see later lectures) Implementing some of these features MAY require that some hidden data is created You would have to set or copy this data if you create or copy the objects, but don t know what it is! Format and implementation NOT defined in the standard for non-pod structs Implementation dependent (probably for speed or efficiency) 8

classes vs structs in C++ classes and structs are (almost) the same thing in C++ The difference is (ONLY!!!) in encapsulation struct defaults to public, class to private Everything you do with a class in C++ could also be done with a struct Sadly, people forget this at exam time Common coding practice in C++: Data only and no member functions: use a struct You get the guarantees about size and positions of member data that you get in a C struct If you add member functions (etc) then use a class Advice: use struct only for C-type structs 9

Methods / member functions In C++, functions can be made class/struct members Just like Java functions #include <cstdio> struct Print { void print() { printf( "Test\n" ); } }; int main() { Print p; p.print(); } Create a struct on stack as in C Call a method on the struct If we had a struct* we would use p->print(); 10

Hiding data inside classes (or structs) Data and methods in a class have either public or private access There is also protected we will see later public methods and data can be accessed by anything Like non-static global functions/data in a file private methods and data can only be accessed by other members of the SAME class Like static global functions/data in a file Note: There is no package level access class members default to private access struct members default to public access 11

Methods/functions and data Data should (usually) be private If it is not, then have a VERY good justification Methods (functions) should be: private for internal use only public for the external class interface The values of the data members comprise the state of the object Interface methods can be: Mutators change the state of the object Accessors only query values, no changes These should really be const functions, which means that the compiler will guarantee they do not alter the state - see lecture 9 Note: inline functions (see later) for methods can ensure that it is (guaranteed) no slower at runtime to use accessors than to use the variable names 12

public and private Keyword private: will change access to private from then onwards Keyword public: will change access to public Note the : at the end class DemoClass { public: int GetValue() { return m_ivalue; } void SetValue( int ivalue ) { m_ivalue = ivalue; } private: int m_ivalue; }; public: for the interface Public from this point onward private: for data and internal functions Private from this point onwards 13

Member functions and data Member data should be private Accessor and mutator functions could be public, including getters and setters This is good practice but not enforced class DemoClass { public: int GetValue() { return m_ivalue; } void SetValue( int ivalue ) { m_ivalue = ivalue; } private: Member data/attributes/state int m_ivalue; }; Methods/member functions/operations Semi-colon at the end 14

Some advance knowledge You can use inheritance, e.g.: class SubClass : public BaseClass { <Data and methods> } Like extends in Java Member functions can access the data in classes or structs There is a hidden this pointer (see lecture 9) Like the hidden this object reference in Java Use this-> not this. static member data and functions work as per Java, shared between instances, no this pointer 15

Constructors and destructors 16

Constructors and Destructors Constructor (as in Java) Called when an object is created Has function name same as class name And no return type (none/empty, NOT void!) Adding a constructor makes it impossible to provide a C-style initialiser. e.g. = {0,1,2}; Look back at the lecture 6 slides on initialisers for structs in C C++11 provides alternatives and relaxes rules Destructor (similar to Java finalize) Called when an object is destroyed A function with name ~ then class name E.g.: ~DemoClass() And no return type 17

Example C++ class class DemoClass { public: DemoClass() { } ~DemoClass() { } Constructor No return type Destructor No return type Accessor Access only, no changes Ideally label the function with keyword const (see later lecture for why) int GetValue() const { return m_ivalue; } void SetValue( int ivalue ) { m_ivalue = ivalue; } private: int m_ivalue; }; Mutator. Mutates/changes the object Data member/member variable/attribute 18

Constructor parameters You can pass parameters to constructors 1. Create a constructor which takes parameters e.g. a constructor which takes an int: DemoClass(int ivalue) { } // In class DemoClass 2. To create an object on the stack, passing values to constructor use: DemoClass mydemoclass(4); You can have multiple constructors Which will differ in which parameter types they expect The compiler will consider which parameters are passed in order to determine which constructor to use In the same way as functional overloading You are probably used to this from Java 19

Default parameters In C++, parameters can have default values Including parameters for constructors Use the = <value> syntax following the parameter declaration e.g.: DemoClass( char* dummy, int ivalue = -1) { /*Nothing*/ } Will match any of the following: DemoClass mydemoclass3( "Temp", 3 ); DemoClass mydemoclass4( "Temp" ); Default values appear only in the function declaration, not any separate definition General C++ rule: if your code introduces ambiguity then it will not compile i.e. compiler cannot work out what to do (could mean A or B) If the constructor that the compiler should call is ambiguous, the code will not compile! Same in Java, but default values make it much more common 20

Default Constructor The Default Constructor is a constructor which can be called with no parameters e.g. one which has no parameters or has default values for all parameters A class can only have one default constructor More would introduce ambiguity Default constructor is used whenever no parameters are specified E.g. When you create arrays of objects, the default constructor is used (because no parameters are provided): e.g.: DemoClass mydemoarray[4]; 21

Constructor parameters or not? Create an object, using default constructor DemoClass mydemoclass1; Or create an object, passing values to the constructor (selects the constructor to use) DemoClass mydemoclass3( "Temp" ); IMPORTANT: Do NOT add empty brackets () when constructing on the stack if there are no parameters! Compiler thinks you are declaring a function e.g. DemoClass mydemoclass1(); // WRONG!!! 22

Aside: Basic types Basic types can be initialised in the same way as classes (using the brackets) Create an int (we have seen this a lot) int ival = 4; // Initialisation! The () form can also be used for basic types int ival(4); // Initialisation! Both do exactly the same thing 23

Initialisation list Initialisation list allows you to pass values to: Data member constructors Base class constructors Uses the () form of initialisation i.e. initialisation values to use are inside () Uses the : operator following the constructor parameters (before the opening brace): DemoClass(int ivalue) : m_ivalue(ivalue) {} Initialisation list, comma separated 24

Example Initialisation List class DemoClass { public: DemoClass( int ivalue ) : m_ivalue(ivalue) { } ~DemoClass() { } int GetValue() { return m_ivalue; } void SetValue(int ivalue) { m_ivalue = ivalue; } private: int m_ivalue; }; 25

Two ways to set member values With an int type data member called m_ivalue Compare the following: DemoClass(int ivalue) : m_ivalue(ivalue) {} With the following: DemoClass(int ivalue) { } m_ivalue = ivalue; Question: Are these the same? 26

Initialisation vs Assignment (1) class DemoClass { public: DemoClass( int ivalue ) : m_ivalue(ivalue) { } Note: You could only have ONE of the following in a class, since they have the same parameters m_ivalue is initialised with value ivalue DemoClass(int ivalue) { m_ivalue = ivalue; } m_ivalue is created but not initialised then the value of ivalue is assigned to it If it was an object (of type struct/class) it would be initialised using default constructor, then assigned i.e. value would be set twice! 27

Initialisation vs Assignment (2) Compare the following: 1) int i = 4; // Initialisation 2) int j; // Uninitialised j = 4; // Assignment Initialisation lists are used a LOT in C++ Should be used in preference to member assignment Not available in Java! In Java you use super() to pass parameters to base class constructor, and then just assign values to members in the constructor In C++ you use the initialisation list for both Initialisation list can be faster in some cases Avoids work from an unnecessary default constructor 28

IMPORTANT Member data is NOT always initialised Basic types and pointers (e.g. int, short or char*) are NOT initialised You should always initialise them Default constructor is called for members of type class/struct unless you say otherwise Using initialisation list BIG WARNING!!!! (I am warning you because the compiler won t!) class MyClass { public: int ai[4]; short j; }; Member data of basic types will be uninitialised Use the initialisation list to initialise variable Use the constructor to set values for arrays The default constructors do nothing 29

Inline functions Member functions and data 30

Inline functions Inline functions act exactly like normal functions but no function call is made (code is put in caller function) Use the keyword inline, e.g.: inline int max( int a, int b ) { return a>b? a : b; } printf("%d\n", max(12,34) ); Similar to a safe macro expansion Safely replaces the function call with the code Unlike a macro (#define) Avoids the overhead of creating a stack frame Code gets included in EVERY file/function which calls it VERY useful for small, fast functions It is advice only: compiler can decide to ignore you 31

Function definitions outside the class In professional code, member functions are usually defined outside of the class declaration In Java they are always defined within the class declaration, with one class per file In C++ you usually have: Function declaration inside class declaration Function definition somewhere else With a label to say it is a class member We use the scoping operator :: to label it Reason: allows hiding of the implementation Good program design, that Java s policy makes very hard to do Defining functions within the class declaration implicitly makes them inline As if they had inline on them 32

Class Declaration and Definition class DemoClass { public: DemoClass( int ivalue = -1 ) : m_ivalue(ivalue) { } ~DemoClass() { } These are all inline functions. Probably do not actually exist as functions in executable Code is included INLINE in the caller function int GetValue() { return m_ivalue; } void SetValue(int ivalue) { m_ivalue = ivalue; } private: int m_ivalue; }; 33

Defining class member functions DemoClass.h class DemoClass { public: DemoClass ( int ivalue = -1 ); ~DemoClass(); int GetValue(); void SetValue( int ivalue); private: int m_ivalue; }; DemoClass.cpp #include "DemoClass.h" DemoClass::DemoClass ( int ivalue ) : m_ivalue(ivalue) { } DemoClass::~DemoClass() { } int DemoClass::GetValue() { return m_ivalue; } void DemoClass::SetValue( int ivalue ) { m_ivalue = ivalue; } 34

Note: Default value DemoClass.h class DemoClass { public: DemoClass ( int ivalue = -1 ); ~DemoClass(); int GetValue(); void SetValue( int ivalue); #include "DemoClass.h" DemoClass::DemoClass ( int ivalue ) : m_ivalue(ivalue) { } DemoClass.cpp Functions in C++ can have default values for parameters. Specify these in the function declaration, not the definition private: int m_ivalue; }; 35

Next lecture References new and delete 36