+ All Categories
Home > Documents > C++ Review (3) Structs, Classes, Data Abstraction.

C++ Review (3) Structs, Classes, Data Abstraction.

Date post: 03-Jan-2016
Category:
Upload: elmer-beasley
View: 222 times
Download: 2 times
Share this document with a friend
Popular Tags:
36
C++ Review (3) Structs, Classes, Data Abstraction
Transcript
Page 1: C++ Review (3) Structs, Classes, Data Abstraction.

C++ Review (3)

Structs, Classes, Data Abstraction

Page 2: C++ Review (3) Structs, Classes, Data Abstraction.

Structs

• An array is a collection of a fixed number of components all of the same data type.

• A struct is a collection of a fixed number of components in which the components are accessed by name, the components may be of different types.

Page 3: C++ Review (3) Structs, Classes, Data Abstraction.

Structs• struct: collection of a fixed number of components,

accessed by name

Page 4: C++ Review (3) Structs, Classes, Data Abstraction.

struct as parameters to Functionsstruct studentType{

String name;int age;double gpa;

};

studentType newStudent;newStudent.name = “Nan Wang”;newStudent.age=18;newStudent.gpa = 4.0;

Write a function to increase student’s age by 1

Page 5: C++ Review (3) Structs, Classes, Data Abstraction.

struct as parameters to Functions

Page 6: C++ Review (3) Structs, Classes, Data Abstraction.

Class

• A structured data type which is specifically designed to– Group data– operations

• Classes: contains class members– Member variables– Member functions

Page 7: C++ Review (3) Structs, Classes, Data Abstraction.

• Class members are private or public– Private members can not be accessed outside the

class (By default, all members of a class are private )– Public members is accessible outside the class

• The word const at the end of the member function specifies that the function can not modify the member variables

Page 8: C++ Review (3) Structs, Classes, Data Abstraction.
Page 9: C++ Review (3) Structs, Classes, Data Abstraction.

#include <iostream>using namespace std;class Square {

int x;public:

int area ();void set_values(int a);int get_sidelen() const;

};int Square:: area () {return (x*x);};void Square::set_values(int a){ x = a; };int Square::get_sidelen(void) const { return x;};int main () {Square s;s.set_values (3);cout << "area: " << s.area();return 0;}

Page 10: C++ Review (3) Structs, Classes, Data Abstraction.

#include <iostream>using namespace std;class Square {

int x;public:

int area ();void set_values(int a);int get_sidelen() const;

};int Square:: area () {return (x*x);};void Square::set_values(int a){ x = a; };int Square::get_sidelen(void) const { return x;};int main () {

Square s; // an object is an instance of a classs.set_values (3); cout << "area: " << s.area();return 0;}

Page 11: C++ Review (3) Structs, Classes, Data Abstraction.

Objects an object is an instance of a class

• Multiple objects of a class can be created (as many as you want).

• All the objects share the same copy of member functions.

• But, they maintain a separate copy of data members.

• Square s1,s2; // each has separate copy of x

Page 12: C++ Review (3) Structs, Classes, Data Abstraction.

Assignment operator

• Square s; • s.set_values (3); • Square s2 = s;• By default, copying a class object is

equivalent to copying all its elements.

Page 13: C++ Review (3) Structs, Classes, Data Abstraction.

Variable assignment

• Values are assigned to variables and not to their data types.

• Hence, you assign values to members of an object and not a class.

• Must create a unique object of a class because you cannot assign values to a class.

• Square = 5 is meaningless…

Page 14: C++ Review (3) Structs, Classes, Data Abstraction.

Accessing member functions

• The member functions in the public section of a class can be accessed using the “.” operator for instantiated objects.

• Only the public members of an object can be directly accessed.Square s;s.set_values(5);cout << s.x; //wrongcout << s.get_sidelen() ; //correct

Page 15: C++ Review (3) Structs, Classes, Data Abstraction.

Special Member functions

• Constructors: Are invoked to initialize the data members of a class.

• Can not return any value (not even void).• Can accept any parameters as needed.

Page 16: C++ Review (3) Structs, Classes, Data Abstraction.

Constructors & Destructors

• Constructors initialize the objects in your class.• Destructors cleans up and frees memory you

may have allocated when the object was created.

Page 17: C++ Review (3) Structs, Classes, Data Abstraction.

Constructors

• Differs from other member functions.• Initializes a newly created object. Use

constructors to guarantee that data members of a class are initialized

• Other member functions are invoked by existing objects.

• A Constructor is invoked automatically when an object is created.

Page 18: C++ Review (3) Structs, Classes, Data Abstraction.

#include <iostream>using namespace std;class Square {

int x;public:

Square(int w) {x = w}; // Constructors have the same name as the class.int area ();void set_values(int a);int get_sidelen() const;

};Int Square:: area () {return (x*x);};void Square::set_values(int a){ x = a; };int Square::get_sidelen(void) const { return x;};int main () {Square s(3);cout << "area: " << s.area();return 0;}

Page 19: C++ Review (3) Structs, Classes, Data Abstraction.

#include <iostream>using namespace std;class Square {

int x;public:

Square() {x =0}; // Default constructor is defined for you: Square(){};int area ();void set_values(int a);int get_sidelen() const;

};int void Square:: area () {return (x*x);};void Square::set_values(int a){ x = a; };int Square::get_sidelen(void) const { return x;};int main () {Square s();cout << "area: " << s.area();return 0;}

Page 20: C++ Review (3) Structs, Classes, Data Abstraction.

Constructors: Overloading• Constructors: Overloading----You can have

several constructors for a class by overloading them.class Square {

int x;public:

Square(int w){ x = w; };Square() { x = 0; };int area () {return (x*x);};void set_values(int a);int get_sidelen(void) const;

};

Page 21: C++ Review (3) Structs, Classes, Data Abstraction.

Constructors: Overloading• If you implement no constructor, the compiler

automatically generates a default constructor for you

• But if you write any constructors at all, the compiler does not supply a default constructor.

Page 22: C++ Review (3) Structs, Classes, Data Abstraction.

Copy Constructors

• The syntax:– class_name(class_name const &source)– Square (Square const &source);

• Const: Making a copy should not alter source.• &: The function should not need to call

another copy constructor!• Square s1(3);• Sequare s2(s1);

Page 23: C++ Review (3) Structs, Classes, Data Abstraction.

Copy Constructors:Example.

class Point {int x,y;

public:int xx(void) const { return x;};int yy(void) const { return y;};Point(Point const &p){

this->x = p.xx();this->y = p.yy();

};};

This pointerUseful when a member

functionmanipulates two or more objects.It holds the address of the

object for which the member function is invoked.

It is always passed to a non-static member function. This ensures the right object is updated using member functions.

Page 24: C++ Review (3) Structs, Classes, Data Abstraction.

Destructors

• You can have many constructors but only one destructor.

• The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no value.

• The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated.

Page 25: C++ Review (3) Structs, Classes, Data Abstraction.

// example on constructors and destructors (from cplusplus.com)#include <iostream>using namespace std;class CRectangle {

int *width, *height;public:

CRectangle (int,int);~CRectangle ();int area () {return (*width * (*height));}

};CRectangle::CRectangle (int a, int b) {

width = new int;height = new int;*width = a; *height = b;

}CRectangle::~CRectangle () { delete width; delete height; }int main () {CRectangle rect (3,4), rectb (5,6);cout << "rect area: " << rect.area() << endl;cout << "rectb area: " << rectb.area() << endl;return 0;

Page 26: C++ Review (3) Structs, Classes, Data Abstraction.

Assignment operators

• Square s1 = s2;• The default behavior : Performs simple

member by member copy. (This is the one generated by the compiler)

• Copy constructor initializes an object.• Assignment operator copies values to an

existing object.

Page 27: C++ Review (3) Structs, Classes, Data Abstraction.

Assignment Operator

• The syntax:– class_name& operator=(const class_name

&source)• Const: Making an assignment should not alter

source.• &: The function should not need to call a copy

constructors.

Page 28: C++ Review (3) Structs, Classes, Data Abstraction.

#include <iostream>#include <string>using namespace std;class Buf{public:

Buf( char* szBuffer, size_t sizeOfBuffer );Buf& operator=( const Buf & );void Display() { cout << buffer << endl; }

private:char* buffer;size_t SizeOfBuffer;

};

Page 29: C++ Review (3) Structs, Classes, Data Abstraction.
Page 30: C++ Review (3) Structs, Classes, Data Abstraction.
Page 31: C++ Review (3) Structs, Classes, Data Abstraction.
Page 32: C++ Review (3) Structs, Classes, Data Abstraction.

Abstract Data Type (ADT)

• Abstraction: separating the design details from its use of called abstraction. E.g. how the car’s engine works is abstraction, how the car’s engine is designed is implementation

• Abstract Data Type (ADT): a data type that separates the logical properties from the implementation details.

• Classes are a convenient way to implement an ADT.

Page 33: C++ Review (3) Structs, Classes, Data Abstraction.

Example• A list is defined as a set of values of the same type. • Because all values in a list are of the same type, a

convenient way to represent and process a list is to use an array.

• You can define a list as an ADT as follows:

Page 34: C++ Review (3) Structs, Classes, Data Abstraction.
Page 35: C++ Review (3) Structs, Classes, Data Abstraction.

Rat In A Maze

Page 36: C++ Review (3) Structs, Classes, Data Abstraction.

In-class test• Define a class, mouse with

– member variables• row: row number• column: column number

– Member functions• moveUp• moveDown• moveLeft• moveRight• getRow• getColumn

• Hints: constructors, destructor etc.• Turn in your paper before you leave.


Recommended