Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

ECE 122. Engineering Problem Solving with Java

Dot and Scope Resolution Operator

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

Computer Programming

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

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Array Elements as Function Parameters

Encapsulation in C++

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example:

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

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

CS24 Week 3 Lecture 1

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

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

CS 247: Software Engineering Principles. ADT Design

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

The University of Nottingham

Constants, References

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

B16 Object Oriented Programming

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

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

Object-Oriented Programming

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

Chapter 4: Writing Classes

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Lecture 10: Introduction to Inheritance

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

C++ Programming Lecture 4 Software Engineering Group

C++ Inheritance and Encapsulation

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

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

Lecture #1. Introduction to Classes and Objects

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CS 101: Computer Programming and Utilization

Functions, Arrays & Structs

Object-Oriented Programming in C++

CS 162 Intro to CS II. Structs vs. Classes

Encapsulation. Mason Vail Boise State University Computer Science

by Pearson Education, Inc. All Rights Reserved. 2

Abstraction in Software Development

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

EL2310 Scientific Programming

CS11 Introduction to C++ Fall Lecture 1

What is an algorithm?

CMSC 341 Lecture 7 Lists

Constructors.

LECTURE 06 FUNCTIONS

Value vs. Entity Objects, Information Hiding

CS24 Week 4 Lecture 1

Principles of Object Oriented Programming. Lecture 4

CMSC 202 Midterm Exam 1 Fall 2015

Abstract Data Types (ADT) and C++ Classes

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

CS11 Intro C++ Spring 2018 Lecture 3

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Friend Functions and Friend Classes

EL2310 Scientific Programming

EECS168 Exam 3 Review

CS250 Final Review Questions

Anatomy of a Class Encapsulation Anatomy of a Method

Chapter 4 Defining Classes I

Introduction to Classes

An Introduction to C++

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

File I/O Christian Schumacher, Info1 D-MAVT 2013

Function Calls and Stack Allocation

CS 103 Unit 10 Slides

Object-Oriented Programming Concepts

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

Computer Programming

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Access Control and Introduction to Classes Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Relevant Topics Structures representing objects Groups of related variables, arrays, other structures Member functions as interfaces for interaction Accessing members (data and functions) of structures No restrictions on accessing members of a structure from anywhere in a program 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture Access control of members in structures private and public members Classes in C++ programs 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Acknowledgment Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

Recap: Object-Oriented Programming Overview Identify entities or objects involved in the working of the system Think of system functionality in terms of operations on and interactions between objects Member functions are interfaces for these operations Abstract away (hide) details of object not necessary to be exposed Data hiding or encapsulation More generally controlling access to information/interface of objects Focus of this lecture Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

Recap: Struct V3 struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); V3 sum (V3 const &b) { V3 v; v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; V3 scale (double const factor) { V3 v; v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Accessing Data Members of V3 int main() { V3 vel, acc, pos; // initial velocity, acceleration, initial position Some more declarations cout << Give x, y and z components of initial velocity: << endl; cin >> vel.x >> vel.y >> vel.z; cout << Give x, y and z components of acceleration: << endl; cin >> acc.x >> acc.y >> acc.z; Rest of code Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Accessing Member Functions of V3 int main() { V3 vel, acc, pos; // initial velocity, acceleration, initial position V3 currdispl, currpos; // current displacement & position double t = 0.0, deltat, totalt; // t: time elapsed so far Reading in and validating values while (t < totalt) { // Calculate current displacement using vel*t + (0.5)*acc*t 2 currdispl = (vel.scale(t)).sum(acc.scale(0.5*t*t)); currpos = currdispl.sum(pos); cout << Time << t << ; currpos.print(); t = t + deltat; return 0; No restrictions on accessing members of a structure from anywhere in a program Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Controlling Access to Members C++ allows three types of access control for every member (data or function) private: Member can be accessed only from member functions of same structure public: Member can be accessed from anywhere in program protected: Outside scope of current discussion Crucial for data hiding or encapsulation private, public, protected: C++ keywords Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Controlling Access to Members struct V3 { double x, y, z; All members of a structure are public by default double length() { return sqrt(x*x + y*y + z*z); V3 sum (V3 const &b) { V3 v; v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; V3 scale (double const factor) { V3 v; v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Controlling Access to Members struct V3 { private: double x, y, z; public: double length() { return sqrt(x*x + y*y + z*z); V3 sum (V3 const &b) { V3 v; v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; ; C++ allows access-control of groups of members V3 scale (double const factor) { V3 v; v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Controlling Access to Members struct V3 { private: double x, y, z; public: V3 sum (V3 const &b) { V3 v; v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; V3 scale (double const factor) { V3 v; v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; ; C++ allows access-control of groups of members private: double length() { return sqrt(x*x + y*y + z*z); Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

C++ keyword Classes in C++ A class is like a structure, except that all members are private by default. More commonly used than structures in C++ programs class V3 { private: double x, y, z; public: double length() {. ; V3 sum(v3 const &b) {. V3 scale(double const factor) {. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Effect of Access Control int main() { V3 vel, acc, pos; // initial velocity, acceleration, initial position Some more declarations cout << Give x, y and z components of initial velocity: << endl; cin >> vel.x >> vel.y >> vel.z; cout << Give x, y and z components of acceleration: << endl; cin >> acc.x >> acc.y >> acc.z; Rest of code Compiler Error! Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

So How Do We Read/Write Data Members of V3? Make all data members public Not preferred, defeats purpose of data encapsulation Breaks modularity of code by exposing internal details E.g., we chose Cartesian coordinates to represent 3-D vectors What if we later decide to use cylindrical coordinates? class V3 { int main() { public: V3 vel, acc, pos; // double x, y, z; // cin >> vel.x >> vel.y >> vel.z; double rho, phi, z; Member functions Rest of code ; cin >> vel.rho >> vel.phi >> vel.z; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Should All Data Members Always Be Private? Not necessarily Expose and allow access to only those members that other functions need access to Hide and prevent access to book-keeping data members, implementation-specific data members, internal state recording data members, Choice of what should be private/public affects quality and modularity of code Relevant for data members and member functions Careful thought process essential more of an art! Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

So How Do We Read/Write Data Members of V3? Accessor functions Member functions that return values of only those data members that other functions are allowed to read class V3 { private: double x, y, z; public: double getx() {return x; double gety() {return y; double getz() {return z; Other member functions ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

So How Do We Read/Write Data Members of V3? Mutator functions Member functions that update values of data members that other functions are allowed to update class V3 { private: double x, y, z; public: void setxyz(double vx, double vy, double vz) { x = vx; y = vy; z = vz; return; Other member functions ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

So How Do We Read/Write Data Members of V3? Changing internal representation of a class requires changing only accessor/mutator function definitions class V3 { private: double rho, phi, z; public: double getx() {return (rho* cos(phi)); double gety() {return (rho* sin(phi)); double getz() {return z; void setxyz(double vx, double vy, double vz) { rho = sqrt(vx*vx + vy*vy); phi = arctan(vy/vx); z = vz; return; Other member functions ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

Summary Controlling access to members in structures through public and private A brief introduction to C++ classes Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20