Classes, objects. Static, const, mutable members of a class

Similar documents
Programming 2. Object Oriented Programming. Daniel POP

Inheritance, and Polymorphism.

AN OVERVIEW OF C++ 1

Programming 2. Object Oriented Programming. Daniel POP

การทดลองท 8_2 Editor Buffer Array Implementation

CS

Abstract Data Types. Different Views of Data:

C++ Strings, Enums. Data Processing Course, I. Hrivnacova, IPN Orsay

CSCE 110 PROGRAMMING FUNDAMENTALS

Operator overloading: extra examples

Intermediate Programming, Spring 2017*

CS24 Week 3 Lecture 1

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

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Name Section: M/W or T/TH. True or False (14 Points)

EL2310 Scientific Programming

Object oriented programming

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

CMSC 202 Midterm Exam 1 Fall 2015

Introduction to C++ Systems Programming

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

Introduction Of Classes ( OOPS )

EL2310 Scientific Programming

Classes, The Rest of the Story

Programming Language. Functions. Eng. Anis Nazer First Semester

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Interview Questions of C++

CS 106B, Lecture 1 Introduction to C++

Overloading operators

Linked List using a Sentinel

CS242 COMPUTER PROGRAMMING

Object Oriented Programming using C++

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Fast Introduction to Object Oriented Programming and C++

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Lecture 23: Pointer Arithmetic

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

Introducing C++ to Java Programmers

Object Oriented Programming COP3330 / CGS5409

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

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

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Variables. Data Types.

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CSCE Practice Midterm. Data Types

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Object oriented programming

Exceptions, Case Study-Exception handling in C++.

Polymorphism Part 1 1

Pointers, Dynamic Data, and Reference Types

SFU CMPT Topic: Classes

Use the dot operator to access a member of a specific object.

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

Polymorphism. Zimmer CSCI 330

Short Notes of CS201

True or False (15 Points)

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Exercise 1.1 Hello world

Comp151. Inheritance: Initialization & Substitution Principle

CS201 - Introduction to Programming Glossary By

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

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

Recharge (int, int, int); //constructor declared void disply();

C++ Programming for Non-C Programmers. Supplement

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CSCI 1370 APRIL 26, 2017

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Classes and Pointers: Some Peculiarities

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Partha Sarathi Mandal

IS 0020 Program Design and Software Tools

Scientific Computing

Chapter 15 - C++ As A "Better C"

Unit 7. 'while' Loops

G52CPP C++ Programming Lecture 13

September 10,

CE221 Programming in C++ Part 1 Introduction

Object Reference and Memory Allocation. Questions:

OBJECT ORIENTED PROGRAMMING

CS 376b Computer Vision

Midterm Exam 5 April 20, 2015

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

C++ For Science and Engineering Lecture 12

Object Oriented Programming: Inheritance Polymorphism

CS 251 Practice Final Exam R. Brown May 1, 2015 SHOW YOUR WORK No work may mean no credit Point totals will be adjusted to a 120 point scale later

Extending Classes (contd.) (Chapter 15) Questions:

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

Lecture on pointers, references, and arrays and vectors

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Transcription:

Classes, objects. Static, const, mutable members of a class Static Members of a class have to be defined and initialized. They are defined only inside the body of the class. -static functions do not allow the this pointer -in static functions we cannot access non-static members -creating, initialization and the access to these members do not depend on the existence of the objects. Example: class Date int day, month, year; static Date today; // declare the static data member // static void inittoday(); ; Date Date::today; // create the static data member Const Members -const data cannot be modified; -the syntax: const data_type data_name; - const functions cannot modify the state of an object -when the function is defined outside the body of the class, then the suffix const is required -the syntax: function_type function_name(list_of_parameters) const; Example: class Date int day, month, year; static Date today; // declare the static data member // const member function int getday() const

day = 0 ; // ERROR: we re in const function return day; int getmonth() const return month; int getyear() const return year; ; int main() Date d; cout << d.getday() << d.getmonth() << d.getyear(); return 0; Mutable Members can be always modified, even in const functions! -it is useful to use mutable in the case when we want to modify a value in a const function -syntax: mutable data_type data_name; Exercises: 1. Write the necessary C++ code such that for the following function main the program will work: int main() cout<<"///////// -----the Math class------\\\\\\\"<<endl; Math ob1(3,4); ob1.times(); ob1.divide(); Math ob2,ob3(5,5);

ob2.times(); cout<<"add "<<ob3.add()<<endl; 2. Create a C++ program which will work for the following function main: int main() Person p1; p1.get_pers(); //will print the name, the city, gender, //the height and how old is the person p1 Person p2("ana Maria","Deva","F",1.63,20); p2.get_pers(); p2.travels(); //the user introduces if the person travels // (Y/N), if Y then it will print where // exactly the person goes and if N //then it will print a message cout<<"the first person is dealing with " <<p1.is_dealing_with()<<endl; p2.eats(); //will print some dishes (depending on the // chosen option) breakfast, lunch or dinner. 3. Repare (add and modify) the following C++ program such that when calling the variable nb_of_objects in the function main will print on the screen the number of objects (as instances of the class Object): 3.3 class Object static int nb_of_objects; Object(int); Object(const Obiect&); ; int Object:: nb_of_objects=0; Object::Object(int a)

Object::nb_of_objects++; Object::Object(const Object& b) cout<<"object: copy constructor "<<endl; Object::nb_of_objects++; Hint: test the following programs (3.1 and 3.2) such that you can see the utility of the keyword static: 3.1 Example of static member: the effect #include <iostream> using namespace std; void printstatic( int c ) static int nstatic; // The value of nstatic is retained // between each call of the function nstatic += c; cout << "nstatic is " << nstatic << endl; int main() for ( int i = 0; i < 5; i++ ) printstatic( i ); Output: nstatic is 0 nstatic is 1 nstatic is 3 nstatic is 6 nstatic is 10 3.2 The use in classes: #include <iostream> using namespace std; class CMyClass

; static int m_i; int CMyClass::m_i = 0; int main() CMyClass::m_i = 1; Output: 0 0 1 1 HOMEWORK 1. Create a class Car, which has a color, engine, horsepower, fuel, year of manufacturing, type, places, other... Take into account the facts that: a car has one single wheel, only 4 tires, can be driven by a single person. Create the corresponding OOP in C++. 2. (Optional) Create a program which contains: a class Student which has name, year, group,..., a class Courses which contains the classes which can attend one student and a class Rooms which contains the rooms where the courses can take place. Note that two ore more courses cannot take place in the same room at the same time. Print the informations about at least 3 students (what are the courses they attend, in which room, ) Use constructors, destructor,... 3. Given: class Date int d,m,y;

int day() const return d; int month() const return m; int year() const; //... ; inline int Date::year() const return y++; //error: attempt to change member value in const function inline int Date::year() // error: const missing in member function type return y; void f(date& d, const Date& cd) int i=d.year();//ok d.add_year(1); //ok int j=cd.year(); //ok cd.add_year(1); //error: cannot change value of const cd Repare the errors!! 4. (Optional) For the following class String write the corresponding constructors, other methods and create some objects. class Sring enum DIM=100; int n; char s[dim]; //constructor ; Read chapter 10 from the book The C++ Programming Language, Bjarne Stroustrup 3rd Edition!!!!!!