Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C++ Review (3) Structs, Classes, Data Abstraction."— Presentation transcript:

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

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

3 Structs struct: collection of a fixed number of components, accessed by name

4 struct as parameters to Functions struct studentType { String name; int age; doublegpa; }; studentType newStudent; newStudent.name = “Nan Wang”; newStudent.age=18; newStudent.gpa = 4.0; Write a function to increase student’s age by 1

5 struct as parameters to Functions

6 Class A structured data type which is specifically designed to – Group data – operations Classes: contains class members – Member variables – Member functions

7 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

8

9 #include 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; }

10 #include 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 class s.set_values (3); cout << "area: " << s.area(); return 0; }

11 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

12 Assignment operator Square s; s.set_values (3); Square s2 = s; By default, copying a class object is equivalent to copying all its elements.

13 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…

14 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; //wrong cout << s.get_sidelen() ; //correct

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

16 Constructors & Destructors Constructors initialize the objects in your class. Destructors cleans up and frees memory you may have allocated when the object was created.

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

18 #include 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; }

19 #include 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; }

20 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; };

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

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

23 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 pointer  Useful when a member function manipulates 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.

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

25 // example on constructors and destructors (from cplusplus.com) #include 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;

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

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

28 #include 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; };

29

30

31

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

33 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:

34

35 Rat In A Maze

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


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

Similar presentations


Ads by Google