CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

Size: px
Start display at page:

Download "CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types"

Transcription

1 CS101: Fundamentals of Computer Programming Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

2 Object- Oriented Programming Model the applica6on/so9ware as a set of objects that interact with each other Objects fuse data (i.e. variables) and func6ons (a.k.a methods) that operate on that data into one item (i.e. object) Like structs but now with associated func6ons/methods Objects become the primary method of encapsula6on and abstrac6on Encapsula6on Hiding of data and implementa6on details (i.e. make so9ware modular) Only expose a well- defined interface to anyone wan6ng to use our object Abstrac6on How we decompose the problem and think about our design rather than the actual code

3 3 Classes Object- oriented design (OOD): a problem solving methodology Objects: components of a solu6on Class: a collec6on of a fixed number of components Member: a component of a class

4 4 Classes Class defini6on: Defines a data type; no memory is allocated Don't forget the semicolon a9er the closing brace Syntax:

5 5 Classes Class member can be a variable or a func6on If a member of a class is a variable It is declared like any other variable You cannot ini6alize a variable when you declare it If a member of a class is a func6on Func6on prototype is listed Func6on members can (directly) access any member of the class

6 6 Classes Three categories of class members: private (default) Member cannot be accessed outside the class public Member is accessible outside the class protected

7 7 Unified Modeling Language Class Diagrams Unified Modeling Language (UML) nota6on: used to graphically describe a class and its members +: member is public -: member is private #: member is protected

8 8 Unified Modeling Language

9 9 Variable (Object) Declara6on Once defined, you can declare variables of that class type clocktype myclock; clocktype yourclock; A class variable is called a class object or class instance

10 10 Accessing Class Members Once an object is declared, it can access the public members of the class Syntax: The dot (.) is the member access operator If an object is declared in the defini6on of a member func6on of the class, it can access the public and private members

11 11 Built- in Opera6ons on Classes Most of C++ s built- in opera6ons do not apply to classes Arithme6c operators cannot be used on class objects unless the operators are overloaded Cannot use rela6onal operators to compare two class objects for equality Built- in opera6ons that are valid for class objects: Member access (.) Assignment (=)

12 12 Assignment Operator and Classes

13 13 Class Scope A member of the class is local to the class Can access a class member outside the class by using the class object name and the member access operator (.)

14 14 Func6ons and Classes Objects can be passed as parameters to func6ons and returned as func6on values As parameters to func6ons Objects can be passed by value or by reference If an object is passed by value Contents of data members of the actual parameter are copied into the corresponding data members of the formal parameter

15 15 Implementa6on of Member Func6ons

16 16 Reference Parameters and Class Objects (Variables) Pass by reference is an efficient way to pass a variable as a parameter Problem: when passing by reference, the actual parameter changes when formal parameter changes Solu6on: use const in the formal parameter declara6on

17 17 Unified Modeling Language

18 18 Implementa6on of Member Func6ons myclock.equaltime(yourclock);

19 19 Implementa6on of Member Func6ons Once a class is properly defined and implemented, it can be used in a program A program that uses/manipulates objects of a class is called a client of that class When you declare objects of the class clocktype, each object has its own copy of the member variables (hr, min, and sec) Called instance variables of the class Every object has its own instance of the data

20 20 Accessor and Mutator Func6ons Accessor func6on: member func6on that only accesses the value(s) of member variable(s) Mutator func6on: member func6on that modifies the value(s) of member variable(s) Constant func6on: Member func6on that cannot modify member variables Use const in func6on heading

21 21 Order of public and private Members of a Class C++ has no fixed order in which to declare public and private members By default, all members of a class are private Use the member access specifier public to make a member available for public access

22 22 Constructors Use constructors to guarantee that member variables of a class are ini6alized Two types of constructors: With parameters Without parameters (default constructor) Name of a constructor = name of the class A constructor has no type

23 23 Constructors A class can have more than one constructor Each must have a different formal parameter list Constructors execute automa6cally when a class object enters its scope They cannot be called like other func6ons Which constructor executes depends on the types of values passed to the class object when the class object is declared

24 24 Constructors clocktype() { hr=0; min=0; sec=0; clocktype(int hour, int minutes, int seconds) { hr=hour; min=minutes; sec=seconds;

25 25 Invoking a Constructor A constructor is automa6cally executed when a class variable is declared Because a class may have more than one constructor, you can invoke a specific constructor

26 26 Invoking the Default Constructor To invoke the default constructor: Example: clocktype yourclock;

27 27 Invoking a Constructor with Parameters clocktype myclock(14,8,25); Number and type of arguments should match the formal parameters (in the order given) of one of the constructors Otherwise, C++ uses type conversion and looks for the best match Any ambiguity causes a compile- 6me error

28 28 Arrays of Class Objects (Variables) and Constructors If you declare an array of class objects, the class should have the default constructor

29 29 Classes and Constructors: A Precau6on If a class has no constructor(s), C++ provides the default constructor However, object declared is s6ll unini6alized If a class includes constructor(s) with parameter(s), but not the default constructor C++ does not provide the default constructor

30 30 Destructors Destructors are func6ons without any type The name of a destructor is the character '~' followed by class name For example: ~clocktype(); A class can have only one destructor The destructor has no parameters Destructor automa6cally executes when the class object goes out of scope

31 31 A struct Versus a class By default, members of a struct are public private specifier can be used in a struct to make a member private By default, the members of a class are private classes and structs have the same capabili6es

32 32 A struct Versus a class In C++, the defini6on of a struct was expanded to include member func6ons, constructors, and destructors If all member variables of a class are public and there are no member func6ons Use a struct

33 33 Classes and Abstract Data Types Abstrac6on Separa6ng design details from usage Separa6ng the logical proper6es from the implementa6on details Abstrac6on can also be applied to data Abstract data type (ADT): data type that separates the logical proper6es from the implementa6on details

34 34 Informa6on Hiding Information hiding: hiding the details of the operations on the data Interface (header) file: contains the specification details File extension is.h Implementation file: contains the implementation details File extension is.cpp In header file, include function prototypes and comments that briefly describe the functions Specify preconditions and/or postconditions

35 35 Informa6on Hiding Implementa6on file must include header file via include statement In include statement: User- defined header files are enclosed in double quotes System- provided header files are enclosed between angular brackets

36 36 Implementa6on of Member Func6ons Must write the code for func6ons defined as func6on prototypes Prototypes are le9 in the class to keep the class smaller and to hide the implementa6on To access iden6fiers local to the class, use the scope resolu6on operator ::

37 Example: Deck of cards Objects contain: Data members Data needed to model the object and track its state/ operation (just like structs) Methods/Functions Code that operates on the object, modifies it, etc. Deck Data members: Array of 52 entries (one for each card) indicating their ordering Top index Deck Methods/Functions Shuffle(), Cut(), Get_top_card()

38 UML (Unified Modeling Language) class name (e.g. Deck) Member functions + shuffle() : void + cut() : void + get_top_card() : int - cards[52] : int - top_index : int Member data class Deck { public: Deck(); // Constructor ~Deck(); // Destructor void shuffle(); void cut(); int get_top_card(); private: int cards[52]; int top_index; ;

39 C++ Classes Programming construct used to define objects, their data members, and methods/functions Similar idea to structs Steps: Define the class data members and method prototypes (usually in a separate header file) Write the methods in a separate.cpp file Instantiate/Declare object variables and use them by calling their methods Terminology: Class = Definition/Blueprint of an object Object = Instance of the class, actual allocation of memory, variable, etc. class Deck { public: Deck(); // Constructor ~Deck(); // Destructor void shuffle(); void cut(); int get_top_card(); private: int cards[52]; int top_index; ; #include<iostream> #include deck.h // Code for each prototyped method #include<iostream> #include deck.h int main() { Deck d; int hand[5]; d.shuffle(); d.cut(); for(int i=0; i < 5; i++){ hand[i] = d.get_top_card(); deck.h deck.cpp poker.cpp

40 Class Definition class name { ; Each function or data member can be classified as public, private, or protected Private: Can call or access only by methods/functions that are part of that class Public: Can call or access by any other code Protected: More on this later Everything private by default so you must use public: to make things visible Make the interface public and the guts/inner-workings private class Deck { public: Deck(); // Constructor ~Deck(); // Destructor void shuffle(); void cut(); int get_top_card(); private: int cards[52]; int top_index; ; #include<iostream> #include deck.h // Code for each prototyped method #include<iostream> #include deck.h int main() { Deck d; int hand[5]; d.shuffle(); d.cut(); d.cards[0] = ACE;//won t compile d.top_index = 5; //won t compile deck.h deck.cpp poker.cpp

41 Constructors / Destructors Constructor is a function of the same name as the class itself It is called automatically when the object is created Use to initialize your object to desired state Returns nothing Destructor is a function of the same name as class itself with a ~ in front Called automatically when object goes out of scope Use to delete any memory allocated by the object Returns nothing class Deck { public: Deck(); // Constructor ~Deck(); // Destructor... ; #include<iostream> #include deck.h Deck::Deck() { top_index = 0; for(int i=0; i < 52; i++){ cards[i] = i; Deck::~Deck() { #include<iostream> #include deck.h int main() { Deck d; // Deck() is called... return 1; // ~Deck() is called since // function is done deck.h deck.cpp poker.cpp

42 Writing Member Functions When writing member functions, we must scope our functions This allows the compiler to check access to private/public variables Without the scope operator [i.e. void shuffle() rather than void Deck::shuffle() ] the compiler would think that the function is some outside function (not a member of Deck) and thus generate an error when it tried to modify the cards array. class Deck { public: Deck(); // Constructor ~Deck(); // Destructor... ; #include<iostream> #include deck.h Deck::Deck() { top_index = 0; for(int i=0; i < 52; i++){ cards[i] = i; Deck::~Deck() { void Deck::shuffle() { cut(); //calls cut() for the object... int Deck::get_top_card() { top_index++; return cards[top_index-1]; deck.h deck.cpp

43 Calling Member Functions Member functions are called by preceding their name with the specific object that it should operate on d1 d1.shuffle() indicates the code of shuffle() should be operating implicitly on d1 s data member vs. d2 or any other Deck object d1 cards[52] top_index cards[52] d top_index #include<iostream> #include deck.h int main() { Deck d1, d2; int hand[5]; d1.shuffle(); // not Deck.shuffle() or // shuffle(d), etc. 0 for(int i=0; i < 5; i++){ hand[i] = d1.get_top_card(); cards[52] top_index

44 Calling Member Functions Within a member function we can just call other member functions directly. d1 s data will be modified (shuffled and cut) d1 is implicitly passed to shuffle() d1 cards[52] top_index cards[52] d top_index 0 Since shuffle was implicitly working on d1 s data, d1 is again implicitly passed to cut() #include<iostream> #include deck.h int main(int argc, char *argv[]) { Deck d1, d2; int hand[5]; d1.shuffle();... #include<iostream> #include deck.h void Deck::shuffle() { cut(); // calls cut() // for this object for(i=0; i < 52; i++){ int r = rand() % (52-i); int temp = cards[r]; cards[r] = cards[i]; cards[i] = temp; void Deck::cut() { // swap 1 st half of deck w/ 2 nd poker.cpp deck.cpp

45 Class Pointers Can declare pointers to classes Use -> operator to access member functions or data d1 cards[52] top_index cards[52] d top_index #include<iostream> #include deck.h int main() { Deck *d1; int hand[5]; d1 = new Deck(); d1->shuffle(); for(int i=0; i < 5; i++){ hand[i] = d1->get_top_card(); delete d1; d1 cards[52] top_index

46 Class Constructors / Destructors Constructor is a function of the same name as the class itself It is called automatically when the object is created (either when declared or when allocated via new ) Use to initialize your object to desired state Returns nothing Destructor is a function of the same name as class itself with a ~ in front Called automatically when object goes out of scope (i.e. when it is deallocated by delete or when scope completes) Use to delete any memory allocated by the object Returns nothing class Deck { public: Deck(); // Constructor ~Deck(); // Destructor... ; #include<iostream> #include deck.h Deck::Deck() { top_index = 0; for(int i=0; i < 52; i++){ cards[i] = i; Deck::~Deck() { #include<iostream> #include deck.h int main() { Deck *dptr; dptr = new Deck(); // Deck() is // called dptr->shuffle(); delete dptr; // ~Deck() is called deck.h deck.cpp poker.cpp

47 Example Student Class Recall unlike structs where other code can access member variables, other code (nonmember functions of the class) cannot access private class members directly class Student { public: Student(); // Constructor 1 Student(string name, int id, double gpa); // Constructor 2 ~Student(); // Destructor... private: string _name; int _id; double _gpa; ; student.h #include<iostream> #include <string> int main() { Student s1; string myname; cin >> myname; s1._name = myname; //compile error...

48 Accessor / Mutator Methods Define public get (accessor) and set (mutator) functions to let other code access desired private data members #include<iostream> #include <string> int main() { Student s1; string myname; cin >> myname; s1.set_name(myname); string another_name; another_name = s1.get_name();... class Student { public: Student(); // Constructor 1 Student(string name, int id, double gpa); // Constructor 2 ~Student(); // Destructor string get_name(); int get_id(); double get_gpa(); void set_name(string s); void set_id(int i); void set_gpa(double g); private: string _name; int _id; double _gpa; ; string Student::get_name() { return _name; int Student:: get_id() { return _id; void Student:: set_name(string s) { _name = s; void Student:: set_gpa(double g) { _gpa = g; student.h student.cpp

49 Multiple Constructors Can have multiple constructors with different argument lists #include<iostream> #include <string> int main() { Student s1; // calls Constructor 1 string myname; cin >> myname; s1.set_name(myname); s1.set_id(214952); s1.set_gpa(3.67); Student s2(myname, 32421, 4.0); // calls Constructor 2 class Student { public: Student(); // Constructor 1 Student(string name, int id, double gpa); // Constructor 2 ~Student(); // Destructor string get_name(); int get_id(); double get_gpa(); void set_name(string name); void set_id(int id); void set_gpa(double gpa); private: string _name; int _id; double _gpa; ; Student::Student() { _name =, _id = 0; _gpa = 2.0; Student::Student(string name, int id, double gpa) { _name = name; _id = id; _gpa = gpa; student.h student.cpp

CS 103 Unit 10 Slides

CS 103 Unit 10 Slides 1 CS 103 Unit 10 Slides C++ Classes Mark Redekopp 2 Object-Oriented Programming Model the application/software as a set of objects that interact with each other Objects fuse data (i.e. variables) and functions

More information

CS 103 Unit 10 Slides

CS 103 Unit 10 Slides 1 CS 103 Unit 10 Slides C++ Classes Mark Redekopp 2 Object-Oriented Programming Model the application/software as a set of objects that interact with each other Objects fuse data (i.e. variables) and functions

More information

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

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 12: Classes and Data Abstraction Objectives In this chapter, you will: Learn about classes Learn about private, protected,

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ 10 Stacks of Coins You have 10 stacks with 10 coins each that look and feel

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation Why use Pointers? Share access to common data (hold onto one copy, everybody

More information

CS 103 Unit 14. Classes Revisited. Mark Redekopp

CS 103 Unit 14. Classes Revisited. Mark Redekopp 1 CS 103 Unit 14 Classes Revisited Mark Redekopp 2 UML (Unified Modeling Language) Shows class definitions in a language-agnostic way Shows class hierarchy (inheritance, etc.) Each class shown in one box

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

Topic 10: Introduction to OO analysis and design

Topic 10: Introduction to OO analysis and design Topic 10: Introduction to OO analysis and design Learning Outcomes Upon successful completion of this topic you will be able to: design classes define class member variables and functions explain public

More information

Methods. Methods. Mysteries Revealed

Methods. Methods. Mysteries Revealed Methods Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private A method (a.k.a. func2on, procedure, rou2ne) is a piece of code

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

CS 101: Computer Programming and Utilization

CS 101: Computer Programming and Utilization CS 101: Computer Programming and Utilization Jul-Nov 2017 Umesh Bellur (cs101@cse.iitb.ac.in) Lecture 14: Object Oritented Programming and Classes About These Slides Based on Chapter 18 of the book An

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17 Announcements Lecture 04b Sept. 14 th, 2017 Midterm #1: Sept. 26 th (week from Tuesday) Code distributed one week from today PA2 test cases & answers posted Quiz #4 next Tuesday (before class) PA3 due

More information

Introduction to Classes

Introduction to Classes Introduction to Classes Procedural and Object-Oriented Programming Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented

More information

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University User Defined Classes CS 180 Sunil Prabhakar Department of Computer Science Purdue University Announcements Register for Piazza. Deleted post accidentally -- sorry. Direct Project questions to responsible

More information

Array Basics: Outline

Array Basics: Outline Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and

More information

CSCI 104 Inheritance. Mark Redekopp David Kempe

CSCI 104 Inheritance. Mark Redekopp David Kempe 1 CSCI 104 Inheritance Mark Redekopp David Kempe 2 Files for Today $ mkdir inh $ cd inh $ wget http://ee.usc.edu/~redekopp/cs104/inh.tar $ tar xvf inh.tar $ make 3 Consider this Struct/Class Examine this

More information

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

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members

More information

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

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 6 Structures and Classes 1 Learning Objectives Structures Structure types Structures as function arguments Initializing structures Classes Defining, member functions Public and private members

More information

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Fall 2012 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

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

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009 CMPT 117: Tutorial 1 Craig Thompson 12 January 2009 Administrivia Coding habits OOP Header Files Function Overloading Class info Tutorials Review of course material additional examples Q&A Labs Work on

More information

Array Basics: Outline

Array Basics: Outline Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

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

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

Department of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List

More information

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

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

Scope. Scope is such an important thing that we ll review what we know about scope now:

Scope. Scope is such an important thing that we ll review what we know about scope now: Scope Scope is such an important thing that we ll review what we know about scope now: Local (block) scope: A name declared within a block is accessible only within that block and blocks enclosed by it,

More information

Special Member Functions

Special Member Functions CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) U Waterloo CS247 (Spring 2017)

More information

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

Classes and Objects. Class scope: - private members are only accessible by the class methods. Class Declaration Classes and Objects class class-tag //data members & function members ; Information hiding in C++: Private Used to hide class member data and methods (implementation details). Public

More information

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Spring 2013 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I Polynomial Class Polynomial(); Polynomial(const string& N, const vector& C); Polynomial operator+(const Polynomial& RHS) const; Polynomial operator-(const Polynomial& RHS) const; Polynomial operator*(const

More information

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

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions Special Member Functions CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) C++

More information

Encapsula)on CMSC 202

Encapsula)on CMSC 202 Encapsula)on CMSC 202 Types of Programmers Class creators Those developing new classes Want to build classes that expose the minimum interface necessary for the client program and hide everything else

More information

C++ Overview (1) COS320 Heejin Ahn

C++ Overview (1) COS320 Heejin Ahn C++ Overview (1) COS320 Heejin Ahn (heejin@cs.princeton.edu) Introduc@on Created by Bjarne Stroustrup Standards C++98, C++03, C++07, C++11, and C++14 Features Classes and objects Operator overloading Templates

More information

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform Ch 13: Introduction to Classes 13.1 Procedural Programming Data is stored in variables CS 2308 Spring 2014 Jill Seaman - Perhaps using arrays and structs. Program is a collection of functions that perform

More information

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND Introduction to Classes 13.2 The Class Unit 4 Chapter 13 CS 2308 Fall 2016 Jill Seaman 1 l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Spring 2015 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Fall 2013 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Func%ons. CS449 Fall 2017

Func%ons. CS449 Fall 2017 Func%ons CS449 Fall 2017 1 Func%ons in C A group of statements performing a task Statements inside func%on is executed by calling it using the () operator Uses: Readability (through code modulariza%on)

More information

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review) Announcements Lecture 05a Sept. 19 th, 2017 9/19/17 CS253 Fall 2017 Bruce Draper 1 Quiz #4 due today (before class) PA1/2 Grading: If something is wrong with your code, you get sympathy PA3 is due today

More information

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

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming Chapter 13: Introduction to Classes 1 13.1 Procedural and Object-Oriented Programming 2 Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a

More information

Mo#va#ng the OO Way. COMP 401, Fall 2017 Lecture 05

Mo#va#ng the OO Way. COMP 401, Fall 2017 Lecture 05 Mo#va#ng the OO Way COMP 401, Fall 2017 Lecture 05 Arrays Finishing up from last #me Mul#dimensional Arrays Mul#dimensional array is simply an array of arrays Fill out dimensions lef to right. int[][]

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

ADTs & Classes. An introduction

ADTs & Classes. An introduction ADTs & Classes An introduction Quick review of OOP Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object

More information

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2

More information

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

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Topics 7.1 Abstract Data Types 7.2 Object-Oriented Programming

More information

CSCI 104 Inheritance. Mark Redekopp David Kempe

CSCI 104 Inheritance. Mark Redekopp David Kempe CSCI 104 Inheritance Mark Redekopp David Kempe Files for Today $ mkdir inh $ cd inh $ wget http://ee.usc.edu/~redekopp/cs104/inh.tar $ tar xvf inh.tar $ g++ -g -o inhtest inhtest.cpp student.cpp person.cpp

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

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

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5 Defining Classes and Methods Chapter 5 Objectives Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters in a method Use modifiers public,

More information

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3 Aykut Erdem, Erkut Erdem, Fuat Akal 1 Today Introduc/on to Programming Basic Concepts Developing Algorithms Crea

More information

Classes in C++98 and C++11

Classes in C++98 and C++11 Classes in C++98 and C++11 January 10, 2018 Brian A. Malloy Slide 1 of 38 1. When we refer to C++98, we are referring to C++98 and C++03, since they differ only slightly. C++98 contained 3 types of constructors,

More information

Fundamental Concepts and Definitions

Fundamental Concepts and Definitions Fundamental Concepts and Definitions Identifier / Symbol / Name These terms are synonymous: they refer to the name given to a programming component. Classes, variables, functions, and methods are the most

More information

Object-Oriented Design (OOD) and C++

Object-Oriented Design (OOD) and C++ Chapter 2 Object-Oriented Design (OOD) and C++ At a Glance Instructor s Manual Table of Contents Chapter Overview Chapter Objectives Instructor Notes Quick Quizzes Discussion Questions Projects to Assign

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND Introduction to Classes 13.2 The Class Unit 4 Chapter 13 CS 2308 Spring 2017 Jill Seaman 1 l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains

More information

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Intermediate Programming & Design (C++) Classes in C++

Intermediate Programming & Design (C++) Classes in C++ Classes in C++ A class is a data type similar to a C structure. It includes various local data (called data members) together with constructors, destructors and member functions. All of them are called

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe and define concepts of class, class object Describe use

More information

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 21 October 21 st, 2015 Transi@on to Java Announcements HW5: GUI & Paint Due Tomorrow, October 22 nd at 11:59pm HW6: Java Programming (Pennstagram)

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3 Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components

More information

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits? Objec,ves Object-oriented programming in Java Ø Encapsula,on Ø Access modifiers Ø Using others classes Ø Defining own classes Sept 16, 2016 Sprenkle - CSCI209 1 Review: Object-Oriented Programming What

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

Classes. OOP & Class Object Terminology File Management

Classes. OOP & Class Object Terminology File Management Classes OOP & Class Object Terminology File Management Object Oriented Programming OOP industry standard C++ is language of choice Object Oriented Programming With OOP and C++ well organized easy to read

More information

Array Basics: Outline

Array Basics: Outline Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and

More information

Problem Solving with C++

Problem Solving with C++ GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter

More information

Recharge (int, int, int); //constructor declared void disply();

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

Lecture #1. Introduction to Classes and Objects

Lecture #1. Introduction to Classes and Objects Lecture #1 Introduction to Classes and Objects Topics 1. Abstract Data Types 2. Object-Oriented Programming 3. Introduction to Classes 4. Introduction to Objects 5. Defining Member Functions 6. Constructors

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1 First half of course SoEware examples From English to Java Template for building small programs Exposure to Java

More information

Writing a Fraction Class

Writing a Fraction Class Writing a Fraction Class So far we have worked with floa0ng-point numbers but computers store binary values, so not all real numbers can be represented precisely In applica0ons where the precision of real

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC CSCI 262 Data Structures 9 Dynamically Allocated Memory POINTERS AND ARRAYS 2 Arrays Arrays are just sequential chunks of memory: Arrays and Pointers Array variables are secretly pointers: x19 x18 x17

More information

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

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a. Intro to OOP - Object and class - The sequence to define and use a class in a program - How/when to use scope resolution operator - How/when to the dot operator - Should be able to write the prototype

More information

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09 Objectives Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters

More information

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

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University (5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University Key Concepts Function templates Defining classes with member functions The Rule

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language

More information

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation. Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements

More information

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; } Objec,ves Inheritance Ø Overriding methods Garbage collec,on Parameter passing in Java Sept 21, 2016 Sprenkle - CSCI209 1 Assignment 2 Review private int onevar; public Assign2(int par) { onevar = par;

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts Chapter 11 Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept

More information

Abstraction in Software Development

Abstraction in Software Development Abstract Data Types Programmer-created data types that specify values that can be stored (type of data) operations that can be done on the values The user of an abstract data type (ADT) does not need to

More information

Implementing an ADT with a Class

Implementing an ADT with a Class Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the

More information

More class design with C++ Starting Savitch Chap. 11

More class design with C++ Starting Savitch Chap. 11 More class design with C++ Starting Savitch Chap. 11 Member or non-member function? l Class operations are typically implemented as member functions Declared inside class definition Can directly access

More information

UNIT III (PART-II) & UNIT IV(PART-I)

UNIT III (PART-II) & UNIT IV(PART-I) UNIT III (PART-II) & UNIT IV(PART-I) Function: it is defined as self contained block of code to perform a task. Functions can be categorized to system-defined functions and user-defined functions. System

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information