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

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

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

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

Ramana Isukapalli W3101: Programming Languages C++

W3110: Programming Languages C++ Ramana Isukapalli

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

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

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


Object Oriented Programming. Solved MCQs - Part 2

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

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Higher Secondary Second Year COMPUTER SCIENCE Model question Paper - 3

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Pointers, Dynamic Data, and Reference Types

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Object Oriented Pragramming (22316)

Fast Introduction to Object Oriented Programming and C++

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

EL2310 Scientific Programming

W3101: Programming Languages C++ Ramana Isukapalli

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

END TERM EXAMINATION

EL2310 Scientific Programming

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

CS349/SE382 A1 C Programming Tutorial

CSE 333 Midterm Exam 2/14/14

CS201 Some Important Definitions

G Programming Languages - Fall 2012

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

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Advanced Systems Programming

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

Programming, numerics and optimization

CS201- Introduction to Programming Current Quizzes

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

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

G52CPP C++ Programming Lecture 13

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

CSE 333 Midterm Exam 7/27/15 Sample Solution

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

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

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

Object Oriented Design

CPSC 427: Object-Oriented Programming

Introduction to Programming Using Java (98-388)

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

COMSW Introduction to Computer Programming in C

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

ONE DIMENSIONAL ARRAYS

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

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

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

C++_ MARKS 40 MIN

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

HOLIDAYS HOMEWORK CLASS : XII. Subject : Computer Science

CS24 Week 3 Lecture 1

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

Procedural Programming & Fundamentals of Programming

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


STRUCTURING OF PROGRAM

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

Downloaded from

1 Pointer Concepts. 1.1 Pointer Examples

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

dynamically allocated memory char* x = new char; int* x = new int[n]; ???...?

CS250 Final Review Questions

CSC231 C Tutorial Fall 2018 Introduction to C

AN OVERVIEW OF C++ 1

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

C++ 8. Constructors and Destructors

Object-Oriented Programming

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

Lecture 10: Introduction to Inheritance

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

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

C++ Classes, Constructor & Object Oriented Programming

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Constructors & Destructors

Intermediate Programming, Spring 2017*

CS250 Final Review Questions

C++ Important Questions with Answers

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Object Reference and Memory Allocation. Questions:

Programming in C++: Assignment Week 1

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED

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

CSE 303: Concepts and Tools for Software Development

Pointers. Developed By Ms. K.M.Sanghavi

Computer Science (330)

Lecture 18 Tao Wang 1

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

6.096 Introduction to C++ January (IAP) 2009

Transcription:

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

Lecture-2 Overview of C continued C character arrays Functions Structures Pointers C++ string class C++ 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 character arrays

C uses character arrays for strings C uses character arrays for strings. C O L U M B I A Useful string functions strlen find the length of a string. strcmp compares two strings Returns 0 if they match. strstr check if a string is sub-string of another string. strcat concatenate two strings. Many others 4 CS3101: Programming Languages: C++

C++ string class

C++ strings C uses char arrays to represent strings char arrays are messy Need to predefine the size of array Size can t be increased easily for longer strings. Copying strings need to use strcpy. C++ strings don t have these issues. E.g. string str1 = abc ; string str2 = str1; string str3 = str1 + pqr ; Much more convenient than C character arrays 6 CS3101: Programming Languages: C++

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

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*/ 8

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. 9

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); 10

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; 11

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 13

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 14 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 16

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. } 17

Initializing member values class Account private: int balance; public: Account ( ) : balance (0) } Account (int amount) : balance (amount) } }; class checkingaccount : public Account checkingaccount (int amount) : Account (amount) } } 18

Class methods Syntax: <ret_type> class::functionname(args) // code } Method code can be present in class definition Outside the class definition In a separate file Example void account::withdrawmoney (int amount) // code 19 }