Encapsulation in C++

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

In Java there are three types of data values:

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

(7 2) Classes: A Deeper Look D & D Chapter 9. Instructor - Andrew S. O Fallon CptS 122 (February 22, 2019) Washington State University

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

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Software design and Implementation 1/6. Software Design and Implementation. Sample Final Exam

Abstraction in Software Development

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Chapter 7. Inheritance

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

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Shared Mutable State SWEN-220

Chapter 4 Defining Classes I

Question 1 Consider the following structure used to keep employee records:

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

ENCAPSULATION AND POLYMORPHISM

CLASSES AND OBJECTS IN JAVA

Friend Functions and Friend Classes

Chapter 6 Introduction to Defining Classes

Introduction to Classes

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming in C#

A Fast Review of C Essentials Part II

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

Some miscellaneous concepts

Inheritance. Reference Text: Chapter11, Big C++

Constants, References

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

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

An Introduction to C++

Java Primer 1: Types, Classes and Operators

CS313D: ADVANCED PROGRAMMING LANGUAGE

Assignment of Structs

Programming II (CS300)

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

Review Questions for Final Exam

Programming 2. Object Oriented Programming. Daniel POP

Programming II (CS300)

Computer Programming

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Class design guidelines. Most of this material comes from Horstmann, Cay: Object-Oriented Design & Patterns (chapter 3)

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1

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

Dot and Scope Resolution Operator

I Wish I Knew How To. Begin Object Oriented Programming With Xojo. By Eugene Dakin. July 2015 Edition (1.0)

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

Principles of Object Oriented Programming. Lecture 4

EEE-425 Programming Languages (2013) 1

Introduction Of Classes ( OOPS )

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

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

CS24 Week 4 Lecture 1

Data Structures (list, dictionary, tuples, sets, strings)

Introduction to C++ with content from

Object-Oriented Principles and Practice / C++

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

#include <iostream> #include <cstdlib>

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

A First Object. We still have another problem. How can we actually make use of the class s data?

CS1004: Intro to CS in Java, Spring 2005

EEE-425 Programming Languages (2013) 1

CS304 Object Oriented Programming Final Term

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

Banaras Hindu University

The Class Construct Part 2

Value vs. Entity Objects, Information Hiding

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Defining Classes and Methods

Mobile Application Programming. Objective-C Classes

Chapter 4. Defining Classes I

The class definition is not a program by itself. It can be used by other programs in order to create objects and use them.

CS 247: Software Engineering Principles. ADT Design

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

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

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

Basic Object-Oriented Concepts. 5-Oct-17

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

Classes and Objects. CGS 3416 Spring 2018

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

G52CPP C++ Programming Lecture 9

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

1/27/2014. OO design approach with examples. UML and project clarifications (2) Project clarifications 1. UML

CS313D: ADVANCED PROGRAMMING LANGUAGE

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

EECS168 Exam 3 Review

by Pearson Education, Inc. All Rights Reserved. 2

JAVA GUI PROGRAMMING REVISION TOUR III

Chapter 14. Inheritance. Slide 1

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Compositional C++ Page 1 of 17

CS250 Final Review Questions

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

Chapter 14 Inheritance. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Transcription:

pm_jat@daiict.ac.in

In abstract sense, it is all about information hiding Informally, you can call it as packaging of data and function together in a single entity called class such that you get implementation independence

C++ has two structuring mechanisms: class and struct that let you specify data and function members have public and private members In a class, members are private by default, while public in struct; for all practical purposes, this is only the difference between struct and class in C++

Data Members Can be static or non-static In C++, non-static data members are called instance variables. Lifetime of instance variables is life of object Scope of data members is within the class, that is all member function of that class Public data members can be accessed through objects using dot operator

static data Members static data members are class data members; that is these variables share common storage for all of its objects For example, you want to have your employee class, such that, It has an additional field emp_no Each time new employee object is created, it automatically gets next employee no! For this we have a class data member next_emp_no, and it is used while constructing new object and incremented, so that new employee no is available for next object construction

data members static data members can be accessed even without having created any object of the class Public static data members can be accessed; for example suppose employee class has organization as a public static data member, then it can accessed- through objects using dot operator harry.organization through class using scope resolution operator Employee::organization

function members - methods Again can be static and non-static Non static functions: are always invoked through objects, also called object methods These functions receive an implicit parameter called this, which is address of the object on which the function has been invoked

function members - methods Static member function can be called without creating any objects of the class Employee::getNextEmployeeNo(); can be called through any object of class emp1.getnextemployeeno(); static member function do not use instance variables of any object these functions do not receive this implicit parameter, therefore access only static data members

accessor and mutator functions Data are kept private.. helps in having implementation independence However most of time you need to know values of data members, for this you have accessor methods, also called as get methodsgetname() getsalary() you have methods that allow you to change values of data members, called mutator methods, also called set methods setsalary( salary ) readconsole()

const member functions Accessor functions are not supposed to modify any data members, and, C++ does not understand which is accessor method and which is mutator method get/set is naming convention and good for human readers However, it allows you to write a const function that can not modify any data memberdouble getsalary() const; void print() const C++ ensures this by making this implicit parameter as constant pointer to such functions

Mutable and immutable data members Data members which can not be changed through out the life of object are immutable In C++, you can have immutable data member by making them constant using const keyword By default data members are mutable, and a constant function can not modify any data member However you can allow them to do so, in some exceptional cases by explicitly making some of variables as mutable mutable int print_count; //suppose you want to have print counter, //now const print method would be able to modify this variable

Example Let us modify our employee class have two private static variables: string organization_name; long next_emp_no; have following static member functionsetorganizationname(string orgname); string getorganizationname() const; long getnextemployeeno() const;

Example Also have a private instance variable: long emp_no; Modify constructors to assign next available employee no to new employee objects, may modify both constructors an accessor method for emp_no: string getemployeeno();

Example Also have print_count a mutable variable in employee class and use it to keep track of times this object has been printed update it in print method - print_count++;

Thanks