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

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

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

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

Ramana Isukapalli W3101: Programming Languages C++


W3110: Programming Languages C++ Ramana Isukapalli

Short Notes of CS201

Abstract classes const member functions const arguments to a function Function overloading and overriding

CS201 - Introduction to Programming Glossary By

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

Object Oriented Programming. Solved MCQs - Part 2

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

C++ Important Questions with Answers

Function Overloading and Overriding this keyword static members inline functions Passing arguments by values and reference

EL2310 Scientific Programming

C++ : Object Oriented Features. What makes C++ Object Oriented

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

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

Object Oriented Design

CS201- Introduction to Programming Current Quizzes

W3101: Programming Languages C++ Ramana Isukapalli

Fast Introduction to Object Oriented Programming and C++

G52CPP C++ Programming Lecture 13

10. Abstract Data Types

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

AN OVERVIEW OF C++ 1

EL2310 Scientific Programming

Programming, numerics and optimization

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

C++ 8. Constructors and Destructors

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


C++ Classes, Constructor & Object Oriented Programming

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

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

END TERM EXAMINATION

Advanced Systems Programming

G Programming Languages - Fall 2012

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

6.096 Introduction to C++ January (IAP) 2009

COMP 2355 Introduction to Systems Programming

Babaria Institute of Technology Computer Science and Engineering Department Practical List of Object Oriented Programming with C

C++ Inheritance and Encapsulation

CS349/SE382 A1 C Programming Tutorial

CS250 Final Review Questions

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

CS201 Some Important Definitions

CS250 Final Review Questions

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Pointers, Dynamic Data, and Reference Types

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

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

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

Object-Oriented Programming

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Data Structures using OOP C++ Lecture 3

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

C++_ MARKS 40 MIN

Lecture 18 Tao Wang 1

Today s lecture. CS 314 fall 01 C++ 1, page 1

C Programming Review CSC 4320/6320

CS24 Week 3 Lecture 1

Interview Questions of C++

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

VIRTUAL FUNCTIONS Chapter 10

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

Lecture 10: Introduction to Inheritance

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

CPSC 427: Object-Oriented Programming

STRUCTURING OF PROGRAM

EL2310 Scientific Programming

CS250 Final Review Questions

Object-Oriented Programming, Iouliia Skliarova

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Chapter 10 Introduction to Classes

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

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

CS304 Object Oriented Programming Final Term

Object Oriented Pragramming (22316)

Introduction to Programming Using Java (98-388)

CS105 C++ Lecture 7. More on Classes, Inheritance

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Midterm Exam 5 April 20, 2015

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Object Reference and Memory Allocation. Questions:

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

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

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

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING


PROGRAMMING IN C++ COURSE CONTENT

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

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Chapter 11. Abstract Data Types and Encapsulation Concepts

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

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

Transcription:

COMS W3101 Programming Language: C++ (Fall 2016) ramana@cs.columbia.edu

Lecture-2 Overview of C C++ Functions Structures Pointers Design, difference with C Concepts of Object oriented Programming Concept of class and Object Constructor and destructor Data and Member functions Data encapsulation public, private and protected members 2

C functions A group of statements To perform a task Possibly return a value Syntax <return_type> fn_name (arguments) // function body } 3

C functions example Example int square (int x) /* fn to compute square*/ return (x * x); } void main() int i = 5; int i_sq = square (5); cout << square of 5: << i_sq << endl; } /* Starting point of ANY C program*/ 4

C pointers A pointer points to a memory location. E.g., int x; /* x is an integer */ int *y; /* y points to an integer */ x = 5; *y = 6; /* NOT y = 6! */ Pointers can point to any data type; int *x; short *y; char *str; double *z; void *p, etc. 5

C pointers, contd. Why do we need pointers? Mainly to manage the memory, as opposed to the compiler managing memory. User needs to assign and delete memory. Allocate memory using malloc. Delete memory using free. Examples int *x = (int *) malloc (sizeof (int)); *x = 3; free (x); 6

C structs C struct used to contain > 1 basic data types Can contain other structs E.g., typedef struct int a, b, c; float x,y,z; } mystruct; mystruct m; m.a = 1; 7

C++

C++ Philosophically different from C High level features of C++ Uses concepts of object oriented programming (OOP) Everything that works in C works in C++ C syntax, operators, structures, control statements, etc. work in C++ Reverse is NOT true Object Oriented Programming Concept of class/object, methods, inheritance, encapsulation, abstraction, polymorphism Key concepts in this Separation of data and methods 9

A simple account example class account private: int user_ssn; // data int accountnumber; // data public: void withdrawmoney (int amount); // method void depositmoney (int amount); // method: void computeinterest( ); // method }; account x; // x is an object of class account 10 W3101: Programming Languages C++

Constructor and Destructor

Constructor and destructor contd. Constructor o A function with the same name as the class o Called when an object is created o A class can have more than one constructor Destructor o Called when an object is cleaned up (goes out of scope) o One class can have only one destructor Examples account x; // constructor code is called account *y = new account; // constructor code is called delete (y); // destructor code is called 12

Constructor and destructor Constructor code account::account( ) user_ssn = -1; accountnumber = -1; } account::account( ) : user_ssn (-1), accountnumber(-1) } account::account (int ssn, int acctnum) user_ssn = ssn; accountnumber = acctnum; } Destructor code account::~account( ) // Any memory/resource cleanup, etc. } 13

Initializing member values class Account private: int balance; public: Account ( ) : balance (0) // same as balance = 0 // in the function body } } Account (int amount) : balance (amount) } }; 14

Class methods Syntax: <ret_type> class::functionname(args) // code } Methods code can be present eitherin class definition In the class defintion or Outside the class definition (possibly in a separate file. E.g., void account::withdrawmoney (int amount) // code } 15

Back to account example class account private: int user_ssn; // data int accountnumber; // data public: account( ); // Constructor-1 account(int m, int n); // Constructor-2 ~account( ); // Destructor void withdrawmoney (int amount); // method void depositmoney (int amount); // method: void computeinterest( ); // method }; 16

Constructor and destructor Constructor code Constructor-1 account::account( ) user_ssn = -1; accountnumber = -1; } // OR account::account( ) : user_ssn (-1), accountnumber(-1) } // Constructor-2 account::account (int ssn, int acctnum) user_ssn = ssn; accountnumber = acctnum; } Destructor code account::~account( ) // Any memory/resource cleanup, etc. } 17

Data encapsulation

Data encapsulation contd. Private members Class Public methods Other classes or functions Output Private members are hidden from other classes, fns. Public Methods act on data to provide output. External classes, functions have access to public methods User should not be affected by Implementation details of public methods. Changes in implementation of methods. 19 CS3101: Programming Languages: C++

Data encapsulation Provide access restrictions to member data and functions From other classes and functions. Implemented y using access modifiers public, private and protected Other classes, functions need to know what methods are implemented Not how they are implemented 20

Account example contd. class has both data and methods. Attributes and methods are members of a class An instance of a class is an object. A class should typically correspond to some meaningful entity. A class uses methods to interact with other classes/functions. private members accessible only to the class (and friends) public members are accessible to every class and functions 21

Back to data encapsulation How can data be hidden? Only class should have access to data Class methods use data Define every class member to be one of public accessible to every the class, other classes, functions and friends private accessible only to class and friends protected accessible only to class, friends and children 22

Data encapsulation in account example In an object of account user_ssn and accountnumber are declared private Accessible only to account objects (and friends) Methods are public Anyone can access them. Example void function1 ( ) // function, not defined in Account account x; x.user_ssn = 123; // Will NOT work x.computeinterest ( ); // Will work } 23