1 C++ Classes (I) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Introduction to Information and Computer Science Computer Programming Lecture e This material (Comp4_Unit5e) was developed by Oregon Health and Science.
1 Classes with Pointer Data Members (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Array, Pointer and Reference ( III ) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures.
The Art of Design Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 Array, Pointer and Reference ( IV ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS230 Lectures Series.
1 Constructors and Destructors Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
Compunet Corporation1 Programming with Visual Basic.NET Creating and Using Classes Lecture # 8 Tariq Ibn Aziz.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
ECE230 Course Introduction Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CS (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts.
Introduction to Object-oriented programming and software development Lecture 1.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 5.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Oriented Programming
Classes and Data Abstraction
Classes and OOP.
OOP-4-Templates, ListType
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
The dirty secrets of objects
Presentation transcript:

1 C++ Classes (I) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series

2 What shall we learn today? What motivates the concept of C++ class? What does it feature? Is it good? An example?

3 What have we learnt about C? C language –Basic data types and syntax –Control structures –Function calls What do you feel about C? –Powerful and flexible –Modular design –All based on function calls –The code are kinda hard to manage and reuse. 

4 C: function-based prog. The way of thinking of a C programmer –thinks of actions (verb.) –is concern about how to produce the outputs given the inputs –makes all sorts of function calls The way of doing of a C programmer –makes all sorts of small “tools” –selects right “tools” s/he built, and uses them to assembly the “data” into a program “Tools” –A tool has a specific “caliber” (the argument list) –To use it, other people need to get a manual (the prototype) –Sometimes, it is hard to use w/o knowing the details of the implementation.

5 Disadvantages It is ok if you only need to write and manage 1,000 lines of code. But what if your have 100,000 lines of code? Code management and re-use are going to be difficult C code only gives you a set of “tools” (functions), i.e., that actions to be taken To build a “house” (the program), you need to prepare “brick”, “wood”, etc. (the data) Obviously, the work is still tremendous!

6 Another Solution? Is there another solution? Yes! –Instead of providing small “tools”, –Why do I provide those “pre-built units” for you to get a “house” done? A “hard-wood” floor A “bedroom” A “kitchen” A “garage” … That would make life much easier!

7 The Change … Changing the way of thinking! –Let’s re-think about our project in terms of “objects”, instead of “actions”. –What “objects” do we need if I want to implement a “MiniMatlab” system? A command line interpreter A system variable database A arithmetic/logic unit Matrix –Why don’t we put “data” and “functions” together to make “packages”? Yes, that sounds a great idea!

8 C++: OOP Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior) into packages called classes Information hiding –Implementation details are hidden within the classes themselves –You, as an end user, don’t need to know the details of implementation. Just use it and enjoy! Classes –Classes are the standard unit of programming –A class is like a blueprint – reusable –Note: we differentiate “class” and “object” –Objects are instantiated (created) from the class –For example, a house is an instance of a “blueprint class”

9 Class = Data + Functions A class is a blueprint of a package –It consists of Data members Describe the attributes of a concept Member functions Describe the behavior of the data –An object is an instantiation of a class A class is abstract An object is real –To use it, you only need to know the “interface” –Some members are accessible, but some aren’t.

10 Example: “variable” How do you describe a “variable”, for example? –“name”? –“value”? –Set a name? –Obtain the name? –Retrieve the value? –Set the value?

11 CVariable class CVariable { doublem_dValue; char*m_sName; public: // constructors and destructors CVariable(); CVariable(const char*name, const double& v = 0.0); ~CVariable(); CVariable(const CVariable& var); // copy constructor const CVariable& operator=(const CVariable& var); // overload = // getting and setting double Value() { return m_dValue; }; char* Name() const { return m_sName; }; void SetValue(const double& v) { m_dValue = v; }; bool SetName(const char* name); };

12 Easy to use! void main() { CVariable a; CVariable b(“var_2”, 10.9); a.SetName(“var_1”); a.SetValue(b.Value() + 1.1); cout << a.Name() << a.Value() << endl; cout << b.Name() << b.Value() << endl; }

13 Example: “varDB” How do you describe a variable DB? –A record? –Size of the DB? –Create and initialize a DB? –Add a record? –Display the DB? –Search the DB? Let’s put them together!

14 CVarDB #defineMAX_SIZE_DB 100 class CVarDB { CVariablem_pDB[MAX_SIZE_DB]; intm_nSize;// size of the database public: // constructors and destructors CVarDB(); ~CVarDB(){}; // interfaces voidInit(); // return a valid ptr if found, else a NULL CVariable*Search(const char*name); // return a ptr of the new one, else a NULL CVariable*CreateANewVar(const char*name); voidDump(); };

15 Life is good! Even w/o looking at the implementation, I can use these class easily! void main() { CVarDB mydb; mydb.Dump(); CVariable *tmpV; tmpV = mydb.CreateANewVar(“var_1”); tmpV->SetValue(10.8); if(mydb.Search(“var_2”)!=NULL){ cout << “found!” << endl; } tmpV = mydb.Search(“ans”); tmpV->SetValue(0.8); mydb.Dump(); }

16 void CVarDB::Init() { m_nSize = 1; m_pDB[0].SetName("ans"); } CVariable* CVarDB::Search(const char* name) { CVariable *pVar = NULL; for(int i=0; i<m_nSize; i++){ if(!strcmp(m_pDB[i].Name(), name)){ pVar = &(m_pDB[i]); break; } return pVar; }

17 CVariable* CVarDB::CreateANewVar(const char*name) { CVariable *pVar = NULL; if(m_nSize < SIZE_DB){ m_nSize ++; m_pDB[m_nSize-1].SetName(name); m_pDB[m_nSize-1].SetValue(0.0); pVar = &(m_pDB[m_nSize-1]); } return pVar; } void CVarDB::Dump() { cout.setf(ios::left, ios::adjustfield); for(int i=0; i<m_nSize; i++){ cout << " " << setw(20) << m_pDB[i].Name() << setw(15) << m_pDB[i].Value() << endl; }

18 A Comparison Let’s compare my C implementation (in Lecture 13) and my C++ implementation of the VarDB!