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

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

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

Introduction Of Classes ( OOPS )

18. Polymorphism. Object Oriented Programming: Pointers to base class // pointers to base class #include <iostream> using namespace std;

Inheritance, and Polymorphism.

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

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

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.

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

Object Oriented Programming. Solved MCQs - Part 2

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

Chapter 1: Object-Oriented Programming Using C++

C++ Important Questions with Answers

C++ Inheritance and Encapsulation

Fast Introduction to Object Oriented Programming and C++

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

Java Object Oriented Design. CSC207 Fall 2014

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

CLASSES AND OBJECTS IN JAVA

C++ (classes) Hwansoo Han

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE


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

Data Abstraction. Hwansoo Han

Object-oriented Programming. Object-oriented Programming

CMSC 132: Object-Oriented Programming II

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

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

Interview Questions of C++

Module 10 Inheritance, Virtual Functions, and Polymorphism

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

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

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

OBJECT ORIENTED PROGRAMMING

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

COEN244: Class & function templates

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Ch02. True/False Indicate whether the statement is true or false.

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

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

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

Object Oriented Programming(OOP).

ITI Introduction to Computing II

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

VALLIAMMAI ENGINEERING COLLEGE

Chapter 9 :: Data Abstraction and Object Orientation

CS-202 Introduction to Object Oriented Programming

Object-Oriented Programming

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

Seminar on Simulation of Telecommunication Sistems. Introduction to object-oriented programming: C++

Midterm Exam 5 April 20, 2015

Object Oriented Programming in C#

6.096 Introduction to C++ January (IAP) 2009

ITI Introduction to Computing II

CS250 Final Review Questions

An Introduction to C++

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

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

Chapter 10 Introduction to Classes

CS250 Final Review Questions

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

Introduction to Classes

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Short Notes of CS201

Data Structures (INE2011)

CS201 - Introduction to Programming Glossary By

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

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Polymorphism. Zimmer CSCI 330

VIRTUAL FUNCTIONS Chapter 10

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Fundamental Concepts and Definitions

Lecture 10: building large projects, beginning C++, C++ and structs

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Lecture 13: more class, C++ memory management

Object Oriented Programming

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

Lecture 18 Tao Wang 1

Lecture 5: Inheritance

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

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

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

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

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

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

G52CPP C++ Programming Lecture 9

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

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

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

CSE 307: Principles of Programming Languages

Chapter 6 Introduction to Defining Classes

C++ Inheritance and Encapsulation

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

Abstraction in Software Development

Framework Fundamentals

CS250 Final Review Questions

Object Oriented Software Design II

Transcription:

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

2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit memory allocation contains methods, such as size()

3 Vectors using vectors must include <vector> template, so must be instantiated with type qualified with std:: can be simplified in small projects

C++ Standard Arrays vs. Vectors 4

5 Vector Length previous program does not check for valid index, which enhances performance using at function will check index

6 Vector Length vectors can grow certain amount of space allocated initially once that space runs out, new space is allocated and the values are copied over

7 Vector Size use pushback(el) to grow the size dynamically use resize to set or reset the size of the array

8 Vector Size use the size() method for loops for (i = 0; i < array.size(); i++) array[i] = 0;

Classes classes fancy struct s expanded concept of data structures (functions) object instantiation of a type/variable class/object defined with keyword class (or struct) 9

10 Classes members are listed under specifiers private members accessible only from within the class protected members accessible to class or classes public members accessible anywhere the object is visible by default, access is

11 Classes example declares a class, Rectangle declares an object, rect class contains 4 members 2 private data 2 public methods (declarations only, not definitions)

12 Classes members are accessed through methods can be accessed directly using. operator similar to struct s

13 Classes example notes: declaration vs. definition inline function encapsulation data hiding output area: 12

14 Classes example with 2 variables notes: each object has its own set of data/methods no parameters needed for call to area output rect area: 12 rectb area: 30

15 Classes what would happen if we called area before setting values? undetermined result constructors automatically called when a new object is initializes values, allocates, etc. constructor name same as class name no return type cannot be called

16 Classes example notes: results same as before set_values omitted values passed to constructor output rect area: 12 rectb area: 30

17 Classes constructors can be different number of parameters different parameter types default constructor defined if no other constructor defined takes no parameters called when object is declared but no parameters are passed to the constructor cannot call default constructor with parentheses represents a declaration

18 Classes member initialization can be done in constructor body or member can be defined normally or with member initialization

19 Classes for types, doesn t matter if initialization is defined or by default for member objects (whose type is a ) if not initialized after the colon, they are defaultconstructed default construction may not be possible if no default constructor defined for class use member initialization list instead

20 Classes example Cylinder class has member of type class Circle and needs to call Circle constructor in member initialization list

21 Classes operator overloading allows operators, such as + or *, to be defined for userdefined types defined like member functions, but prepended with keyword operator

22 Classes operator overloading example example: equivalent

Classes this pointer to used within a class method to refer to the object that called it example Rectangle::Rectangle (int width, int height) { this -> width = width; this -> height = height; } 23

24 Classes templates parameterized class can be used to store elements of type int or type float

25 Classes destructor opposite of constructor called when an object s ends performs, such as memory deallocation returns nothing, not even void name same as class name, but preceded by ~ implicit default destructor provided if none defined

26 Classes destructor example

27 Inheritance inheritance allows classes to be classes retain characteristics of the base class avoids replicated code by allowing common properties to be contained in one class and then used by other classes Polygon contains common members; Rectangle and Triangle contain common members plus specific features

Inheritance inheritance example derived classes contain width, height, set_values output 28

29 Inheritance inheritance access types and inheritance inherited members have same access permissions as in base class since

30 Virtual Methods virtual methods can be redefined in classes, while preserving its calling signature declared with keyword virtual

31 Virtual Methods virtual method example area declared virtual derived classes will redefine it

32 Virtual Methods virtual methods if virtual keyword removed, all derived class calls to area method through pointers to base class would return 0 virtual methods redefined in derived classes non-virtual methods can also be redefined in derived classes but, if virtual, a to the base class can access the redefined virtual method in the derived class a class that declares or inherits a virtual function is note that Poly is a class, too, and objects can be declared with it

33 Virtual Methods abstract base class similar to base class in previous example can only be used as base classes can have virtual methods without pure virtual function appended with =0

34 Classes abstract base class cannot be used to declare can be used to create to it and take advantage of polymorphic features

35 Inheritance abstract base class example