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

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

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

Introduction Of Classes ( OOPS )

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

Lecture 18 Tao Wang 1

Derived Classes in C++

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

CLASSES AND OBJECTS IN JAVA

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

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Object Oriented Programming in C#

C++ (classes) Hwansoo Han

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

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

COEN244: Class & function templates

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

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Chapter 10 Introduction to Classes

What is Class? Remember

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

Chapter 4 Defining Classes I

An Introduction to C++

CSE 303: Concepts and Tools for Software Development

Object-Oriented Principles and Practice / C++

G52CPP C++ Programming Lecture 9

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

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

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

Introduction To C#.NET

Chapter 11. Abstract Data Types and Encapsulation Concepts

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Constructors for classes

Introduction to Programming Using Java (98-388)

Interview Questions of C++

Object-Oriented Principles and Practice / C++

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

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

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Object oriented programming with C++

C++ Important Questions with Answers

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

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

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

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Programming, numerics and optimization

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Logistics. Final Exam on Friday at 3pm in CHEM 102

Intermediate Programming & Design (C++) Classes in C++

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Classes, Constructors, etc., in C++

CS313D: ADVANCED PROGRAMMING LANGUAGE

Classes. Christian Schumacher, Info1 D-MAVT 2013

Programming II (CS300)

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

CS193D Handout 10 Winter 2005/2006 January 23, 2006 Pimp Your Classes

CMSC 132: Object-Oriented Programming II

Programming II (CS300)

Object Oriented Programming. A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

Protection Levels and Constructors The 'const' Keyword

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Intermediate Programming, Spring 2017*

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

VIRTUAL FUNCTIONS Chapter 10

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

CS24 Week 3 Lecture 1


Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Special Member Functions

CS313D: ADVANCED PROGRAMMING LANGUAGE

AIMS Embedded Systems Programming MT 2017

EL2310 Scientific Programming

CS304 Object Oriented Programming Final Term

CS 162, Lecture 25: Exam II Review. 30 May 2018

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Implementing Abstractions

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Object Oriented Software Design II

A brief introduction to C++

Programming in C and C++

C++ Inheritance and Encapsulation

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

QUIZ Friends class Y;

Ch. 12: Operator Overloading

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Assignment of Structs

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Software Design II

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

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

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Lecture 5: Inheritance

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Transcription:

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

Outline C++ Classes Special Members Friendship

Classes are an expanded version of data structures (structs) Like structs, the hold data members They also hold functions as members Can specify access permissions also (Sounding a lot like Java, right?) Defined with the keyword class :

Access Specifiers: private Only accessible from within members of the same class or from friends protected Private access plus members of derived classes can have access public Accessible anywhere the object is visible Default access is private

An example: To access data and function members:

The full Rectangle example:

The Scope Operator: Used to define a member function of a class outside of the class definition area() is defined within the class set_values() is defined outside the class Scope operator (::) specifies the class to which the function belongs

Classes (just like structs) define a data type As always, we can many objects of the class type And each object will have its own member variables and functions that operate on those variables

Constructors Used to create a new object of the class data type Initializes any member variables that need to be initialized May do other work if needed Just like in Java, the constructor function name is the same as the class name They cannot be called like regular member functions They are only executed once when the object is instantiated They have no return values not even void

Constructor Example Classes

Overloading constructors Classes can have more than one constructor All named the same, since they are constructors But with different parameter lists Remember the method signature in Java? The default constructor is called when an object is declared but a constructor is not specified This is different than a constructor with no parameters An example would be appropriate here

Member initialization C++ offers a cleaner way of initializing member variables than does Java

A subtle constructor example

Pointers to Classes

When you define a class, you are defining a new data type, just like in Java This includes the member data and the operators on that data Unlike Java, you can overload symbolic operators These are defined with regular functions with a special name: operator

Overloaded operators The parameter(s) expected for an overloaded operator are shown below (the @ sign is just a place holder for the actual operator)

Overloaded operators can be member functions or not!

this Just like Java, C++ uses the keyword this to refer to itself Difference is, this is a pointer

Static members Just like Java, a static member means there is only one common variable for all objects of that class

Constant member functions (const) When an object is instantiated by const, the access to its data members outside the class is read only A constructor is still called to initialize variables but can only be called once

Const Member functions specified as const cannot modify non-static data members They cannot call other non-const member functions Member functions can be overloaded based on const or not You can have two functions of the same name, with one const, and one not (example next slide)

Const overloading Classes

Template Classes Classes

To use template classes Classes

Special Member Functions

Default Constructor The constructor called when objects are declared but not initialized with any parameters This is supplied automatically But just like Java, if a constructor of any kind is defined, the default constructor is no longer valid

Default Constructor Classes

Destructors The opposite of a constructor They clean up any memory used by the object before deleting it Same name as class/constructor, but starts with a ~

Copy Constructor Default copy constructor does a shallow copy Deep copy is done when pointer contents, not just addresses, are also copied

Copy Assignment Gives you the ability to copy objects not just on construction, but at any time Really overloading the = operator

Summary C++ Classes Special Members Friendship