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

Preview:

Citation preview

1

C++ Classes (I)

Ying Wu Electrical Engineering & Computer

ScienceNorthwestern Universityyingwu@ece.northwestern.edu

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” floorA “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 interpreterA system variable databaseA arithmetic/logic unitMatrix

– 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 classA class is abstractAn 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

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

};

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#define MAX_SIZE_DB 100class CVarDB{

CVariable m_pDB[MAX_SIZE_DB];int m_nSize; // size of the database

public:// constructors and destructorsCVarDB();~CVarDB(){};

// interfacesvoid Init();

// return a valid ptr if found, else a NULLCVariable* Search(const char*name);

// return a ptr of the new one, else a NULLCVariable* CreateANewVar(const char*name);

void Dump();};

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;

}

voidCVarDB::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!

Recommended