An Introduction to C++

Similar documents
Object-Oriented Programming

Interview Questions of C++


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

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

Lecture 18 Tao Wang 1

Abstract Data Types and Encapsulation Concepts

CMSC 4023 Chapter 11

OBJECT ORIENTED PROGRAMMING USING C++

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

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

CS201 Some Important Definitions

Implementing Subprograms

Chapter 10 Introduction to Classes

Programming in C and C++

Short Notes of CS201

Programming, numerics and optimization

Object Oriented Design

Pointers, Dynamic Data, and Reference Types

CS304 Object Oriented Programming Final Term

CS201 - Introduction to Programming Glossary By

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

Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

10. Abstract Data Types

Cpt S 122 Data Structures. Introduction to C++ Part II

A brief introduction to C++

Introduction to Programming Using Java (98-388)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

STRUCTURING OF PROGRAM

Object-Oriented Programming

Chapter 4: Writing Classes

C++ Object-Oriented Programming

Motivation was to facilitate development of systems software, especially OS development.

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

Common Misunderstandings from Exam 1 Material

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

CSC1322 Object-Oriented Programming Concepts

G52CPP C++ Programming Lecture 9

Object Oriented Programming. Solved MCQs - Part 2

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Launchpad Lecture -10

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CLASSES AND OBJECTS IN JAVA

IBS Technical Interview Questions

Programming II (CS300)

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Advanced Systems Programming

Programming II (CS300)

The C++ Object Lifecycle. EECS 211 Winter 2019

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

CS201- Introduction to Programming Current Quizzes

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

CS1004: Intro to CS in Java, Spring 2005

Fast Introduction to Object Oriented Programming and C++

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

CMSC 132: Object-Oriented Programming II

Chapter 6 Introduction to Defining Classes

September 10,

2 ADT Programming User-defined abstract data types

Chapter 11. Abstract Data Types and Encapsulation Concepts

Evolution of Programming Languages

CGS 2405 Advanced Programming with C++ Course Justification

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Topic 10: Introduction to OO analysis and design

CS24 Week 3 Lecture 1

Abstraction in Software Development

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

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Chapter 11. Abstract Data Types and Encapsulation Concepts

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

CMSC 132: Object-Oriented Programming II

AN OVERVIEW OF C++ 1

Where do we go from here?

COMP 2355 Introduction to Systems Programming

Data Abstraction. Hwansoo Han

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Object Oriented Programming COP3330 / CGS5409

Motivation was to facilitate development of systems software, especially OS development.

Abstract data types &

Abstract Data Types & Object-Oriented Programming

Objects Managing a Resource

Queue ADT. CMPT 125 Feb. 28

CS11 Intro C++ Spring 2018 Lecture 1

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

CE221 Programming in C++ Part 1 Introduction

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

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

More C++ : Vectors, Classes, Inheritance, Templates

EMBEDDED SYSTEMS PROGRAMMING OO Basics

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Programming II (CS300)

Object-Oriented Principles and Practice / C++

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Transcription:

An Introduction to C++

Introduction to C++ C++ classes C++ class details

To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface In the.c file Define function implementations Implementation is kept separate from the interface

Data and operations (functions) are still separate to some extent It is still open to misuse by errant programmers As direct access to struct data is still possible LL_t* ll = LLcreate(); ll->head->next->next->next = ll->head->next; One solution: classes Which do not exist in C

C++ evolved from C Created by Bjarne Stroustrup in 1978 Motivated by interface issues Provides constructs to support Information hiding Encapsulation of data and functions (methods) Common situations for code re-use

Classes encapsulate both data and operations Functions that belong to a class are referred to as methods Class data and methods can explicitly be made public or private Which prevents programmers using a class to access its implementation details Syntactically enforcing information hiding Classes can be inherited Which we will not discuss in CMPT 125

There are many differences between C and C++ C++ has many libraries that incorporate classes Such as a string class The bool data type Template classes and functions Exception handling Different pointer types A feature of modern C++ (C++11) We do not have time to look at all these features But we will briefly discuss memory management

We can use malloc and calloc to allocate dynamic memory in our C++ programs Don't C++ has its own syntax for allocating and deallocating dynamic memory To allocate dynamic memory use new int* arr = new int[10]; Node* nd = new Node; To deallocate dynamic memory use delete delete[] arr; delete nd; The compiler figures out how much space is needed The []s are needed to delete an array

A class provides the definition of a complex datatype and its operations A class is a type definition And is used in much the same way as base types (int, char, etc.) Creating a class does not create class variables Class variables are called objects Creating a new object of a class is referred to as instantiating an object The process is referred as instantiation

C++ classes are typically broken down into two files The class definition is in a.h file Contains class variables (properties) And method prototypes The method implementations are in a.cpp file Which #includes the.h file Method implementations are preceded by the class name and the scope resolution operator, :: their fully qualified names class LinkedList { // }; void LinkedList::append(int val) { // }

Classes have special methods that are used to instantiate objects Called constructors Constructors give class properties appropriate values That respect class invariants Constructors have the same name as the class and no return type A class can have multiple constructors With different parameter lists An example of function overloading class LinkedList { //constructor LinkedList(); If no constructor is defined for a class the compiler creates a default constructor

In C++ the programmer decides whether objects are created on the stack or the heap LinkedList ll; Creates a linked list on the stack Not the lack of brackets in the default constructor call LinkedList* ll2 = new LinkedList(ll); On heap Creates a copy of a linked list using the copy constructor Note that different constructors have the same name but different parameter lists Because the parameter lists are different there is no ambiguity

A copy constructor allows us to create a copy of an existing object Its sole parameter is the object to be copied Passed as a constant reference C++ helpfully auto-generates a copy constructor if a class doesn t have one However it is often necessary to create your own copy constructor We will discuss this later

A class definition is divided into public and private sections And some times protected relating to inheritance Public attributes and methods can be accessed by non-class objects and functions i.e. from outside the class Private attributes and methods can only be accessed inside the class That is, within the implementation of class methods

The private section of a class relates to its implementation and data Class data is generally made private Making class data private has two useful effects It allows the implementation to be changed without also changing the interface It protects class data from being given inappropriate values In addition to data, helper methods should also be made private

The public section of a class makes up its interface A set of methods that define the class operations Only methods that are required to be accessed from outside the class should be made public Since class data is private it can only be accessed through methods but can be directly accessed from within the class Setter methods change data Getter methods access data also known as mutators also known as accessors

The interface should be public and the implementation private This allows the implementation to be protected from inappropriate changes Typically, class attributes should be made private And only changed through public methods Making the implementation private allows it to be changed Without affecting programs using the class

A setter method sets the value of a class attribute Setters should respect class invariants That is they should not allow class attributes to be given inappropriate values Such as radius never being negative If an invalid value is passed to a setter it should do something about it what? Change the method to return a boolean? Assign a default value? Throw an error?

class Point { public: Point(); Point(int x1, int y1); void setpoint(int x1, int y1); int getx(); int gety(); private: int x; int y; }; void Point::setPoint(int x1, int y1) { x = x1; y = y1; } What's the point of the setter?

C++ provides new and delete instead of malloc and free to allocate and deallocate dynamic memory It is particularly important to use new and delete when creating or destroying objects In addition to allocating space on the heap and returning its address new also calls the appropriate constructor In addition to deallocating space delete also calls the class destructor What's a destructor? A special function responsible for cleaning up memory associated with an object

Class programmers have responsibilities relating to the use of dynamic memory If a class allocates space in dynamic memory to the class properties the programmer must Write a destructor Write a copy constructor Write an overloaded assignment operator Failure to do may result in undesired results

A destructor is responsible for cleaning up dynamic memory allocated to an object The destructor should call delete on any variable allocated space in dynamic memory For a linked list this would entail traversing the list and calling delete on each node Destructors have a particular prototype The name of the class preceded by a tilde ~LinkedList(); Destructors are never explicitly called But are invoked when delete is called on an object

A copy constructor creates an object that is a copy of an existing object If a copy constructor is not created one is automatically created But it only make a shallow copy LinkedList ll2(ll1); Copies the addresses not the nodes! ll1.head 45 ll2.head ll2.tail 29 13 42 NULL ll1.tail

A copy constructor for a class that allocated dynamic memory should make a deep copy of an object A deep copy creates a copy of data stored on the heap Instead of making copies of addresses to the data A copy constructor for a linked list would traverse through the original list Calling new to create a copy of each node to build a separate and complete list And setting the head and tail of the new linked list object to the addresses of the start and end of the new list

The copy constructor and destructor address most of the issues with classes and dynamic memory The destructor deallocates dynamic memory allocated to an object The copy constructor deals with Explicitly creating a copy of an existing object By calling the constructor Passing an object by value to a function parameter Which calls the copy constructor to create the new object What happens when one object is assigned another?

LinkedList ll1; LinkedList ll2; // Populate lists and work with ll1 and ll2 What happens? ll1 = ll2; Presumably the intent is to make ll1 a copy of ll2 Destroying the original contents of ll1 in the process Looks very similar to what the copy constructor does But ll1's copy constructor is not called in this situation Why not? Solution: overload the assignment operator

C++ allows its operators to be overloaded Have their operations defined for use with non base-type variables Much like the copy constructor the assignment operator creates a shallow copy Unless the class programmer explicitly overloads the assignment operator to make a deep copy The overloaded assignment operator should also clean up memory associated with the original object