Classes Nov 3, Ch

Similar documents
Highlights. - recursion. - classes

Structs/classes Ch

Classes Ch

Pointers Ch 9, 11.3 & 13.1

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

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

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Ch. 10: Name Control

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

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

Introduction to C++ with content from

Object Oriented Programming

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

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

QUIZ Friends class Y;

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

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

Inheritance Ch

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

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

by Pearson Education, Inc. All Rights Reserved. 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

C++ Optimization Activity

Object Oriented Design

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

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Control Structures. Important Semantic Difference

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance)

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

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Variables. Data Types.

#include <iostream> #include <cstdlib>

Encapsulation in C++

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

EECS 280 C++ Coding Standards

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

A A B U n i v e r s i t y

Introduction To C#.NET

C++ : Object Oriented Features. What makes C++ Object Oriented

Variables and numeric types

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

G52CPP C++ Programming Lecture 9

C++ Classes & Object Oriented Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Introduction to Classes

CSE 303: Concepts and Tools for Software Development

Your first C++ program

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

The Stack, Free Store, and Global Namespace

7. C++ Class and Object

Syntax and Variables

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Data Structures using OOP C++ Lecture 3

CS32 Summer Intro to Object-Oriented Programming in C++ Victor Amelkin August 12, 2013

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Introduction to Functions

Abstract Data Types. Different Views of Data:

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

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

Friends and Overloaded Operators

Lecture 13: more class, C++ memory management

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Fast Introduction to Object Oriented Programming and C++

CSCI 102 Fall 2010 Exam #1

Introduction Of Classes ( OOPS )

Friends and Overloaded Operators

APCS Semester #1 Final Exam Practice Problems

d2vbaref.doc Page 1 of 22 05/11/02 14:21

Data Structures and Other Objects Using C++

9 Working with the Java Class Library

Chapter 1 Getting Started

QUIZ. How could we disable the automatic creation of copyconstructors

Messages, Instances and Initialization

UEE1302 (1102) F10: Introduction to Computers and Programming

C++ For Science and Engineering Lecture 12

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

6.096 Introduction to C++

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

Fundamentals of Programming Session 25

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lecture 02, Fall 2018 Friday September 7

How to approach a computational problem

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

Transcription:

Classes Nov 3, Ch 10.1-10.3

Highlights - class / struct (structure) - constructor

struct/class vs array Arrays group together similar data types (any amount you want) Classes and structs group together dissimilar types that are logically similar

class / structs classes and structs are outlines/blue prints of an organization structure Thus when you create a variable of your class's type, you create an instance

struct Suppose you wanted to write a function to find the maximum element in an array How would you return both an index and the element?

struct Suppose you wanted to write a function to find the maximum element in an array How would you return both an index and the element? 1. Use a global variable to share between functions 2. Use call-by-reference (See: findmax.cpp)

struct A struct (structure) is a grouping of similar objects (See: findmaxv2.cpp)

struct You just made your own data type (just like int/double/char/etc.) You can make as many variables of this type as you want The dot operator tells the computer to go inside the object/container

struct You need the dot to differentiate between two different variables You can also think of the dot as possessive in English (. 's )

struct You can initialize a struct using braces (much like arrays, goes in order declared) You can also use = to assign all elements... same as...

class / structs Suppose we are planning to redo all the classroom name plates in Keller hall How would you store all the room information...... without structs?... with structs? (See: room.cpp)

class A class is functionally the same as a struct (creates a new data type) However, the notation is slightly different (contains functions)

class You can put const to the right of the function in a class to designate that it will not change any of the member variables const means cannot change day, month or year

class and :: To define a class functions, we need to specify the scope using :: (scope resolution)... compared to... (See: date.cpp)

:: Scope resolution is actually what namespaces are for: Using the aboves lets us write:... instead of... annoying to rewrite every time

:: vs. The :: is very similar to the. operator :: is used to specify the location in a general sense (without a specific variable involved) Example: Put socks on before shoes. is used to specify the ownership of a variable or function (owner is another variable) Example: Tie my shoe laces specific case

class / structs classes and structs make code much easier to modify in addition to organize Learning how to write code is practice, this will become natural if you do it a lot Writing code that can easily be added to is much more difficult

class class inside of another class? Sure, why not!

class Suppose we wanted to change the HORRIBLE floor-to-number convention in Keller Hall (thus the re-plating of classrooms) Break up the rooms by floor and have a floor label for each floor We can keep the same number scheme: nd 2-130 is 2 floor room number 130 (See: building.cpp)

public vs private

public vs private The public keyword allows anyone anywhere to access the variable/method The private keyword only allows access in the class where the variable/method is defined

public vs private All variables should be private While this means you need methods to set variables, users do not need to know how the class works This allows an easier interface for the user (also easier to modify/update code) (See: dateprivate.cpp)

public vs private

public vs private Creating interfaces with public allows users to not worry about the private implementation So... more work for you (programmer) less work for everyone else

Constructors The date class has two functions: setdate() and print() As we need to run setdate() on a variable before it is useful anyways In fact, such a thing exists and is called a constructor (run every time you create a variable)

Constructors The class name and the constructor must be identical (constructors also have no return type) (See: dateconstructor.cpp)

Constructors If you don't put a constructor, C++ will make a default constructor for you (no arguments) default constructor To use the default constructor say this:... not this:

Constructors If you declared constructors you must use one of those Only if you declare no constructors, does C++ make one for you (the default) Note: our dateconstructor.cpp has no way to change the value of the date after it is created (thus gives control over how to use class)

#include Just as writing very long main() functions can start to get confusing...... writing very long.cpp files can also get confusing Classes are a good way to split up code among different files

#include You can #include your class back in at the top or link to it at compile time You have to be careful as #include basically copies/pastes text for you Will not compile if class declared twice (used in two different classes you #include)

#include To get around this, you can use compiler commands in your file if not defined define This ensures you only have declarations once (See: dateclass.hpp, dateclass.cpp, rundate.cpp)