1 / 19

C++ Class (I)

ECE230 Lectures Series. C++ Class (I). Ying Wu Electrical & Computer Engineering Northwestern University yingwu@ece.northwestern.edu. What shall we learn today?. What motivates the C++ language? What does it feature? Is it good? An example?. What have we learnt about C?. C language

jaser
Download Presentation

C++ Class (I)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE230 Lectures Series C++ Class (I) Ying Wu Electrical & Computer Engineering Northwestern University yingwu@ece.northwestern.edu

  2. What shall we learn today? • What motivates the C++ language? • What does it feature? • Is it good? • An example?

  3. What have we learnt about C? • C language • Basic data type and syntax • Control structures • Function call • 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. An example of C • Let’s see the MP#3 • How many small tools do we need? • ReadAPiece() • ResolveACommandLine()? • IsCharADigit() • CopyString() • CreatVarDB() • SearchVarDB() • AssignAName() • DumpVarDB() • … • Yeap, you may have struggled and finally made it work.  • But, is it good for re-use? • I shall ask you: even if I gave you my C code of MP#3 and let you add the functionality of making the matrix cases work, are you confident to get it done in a couple of days? • You know the answer. 

  6. Why? • Because my 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!

  7. A Solution? • Is there a 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!

  8. In terms of our job … • Changing the way of thinking! • Let’s re-think about our project in terms of “objects”, instead of “actions”. • What “objects” do we need for the MiniMatlab? • 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!

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

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

  11. Example: “variable” • How do you describe a “variable” in our MPs? • “name”? • “value”? • Set a name? • Obtain the name? • Retrieve the value? • Set the value?

  12. CVariable class CVariable { double m_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); };

  13. 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; }

  14. Example: “varDB” • How do you describe a var 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!

  15. CVarDB #define MAX_SIZE_DB 100 class CVarDB { CVariable m_pDB[MAX_SIZE_DB]; int m_nSize; // size of the database public: // constructors and destructors CVarDB(); ~CVarDB(){}; // interfaces void Init(); // 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); void Dump(); };

  16. 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(); }

  17. void CVarDB::Init() { m_nSize = 1; m_pDB[0].SetName("ans"); } // NOTE: return a ptr of the variable if found 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; }

  18. 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; } }

  19. A Comparison • Let’s compare my C implementation and my C++ implementation of the VarDB!

More Related