Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures and Classes. Motivation  Types we discussed so far Basic types: char, int, float, double Arrays: a collection of indexed-variables with the.

Similar presentations


Presentation on theme: "Structures and Classes. Motivation  Types we discussed so far Basic types: char, int, float, double Arrays: a collection of indexed-variables with the."— Presentation transcript:

1 Structures and Classes

2 Motivation  Types we discussed so far Basic types: char, int, float, double Arrays: a collection of indexed-variables with the same type  To model real-life types, we need to be able to create user-defined collection of data  Sometimes, we also want to defined operations on this data  C++ offers two ways: Structures: group together variables of possibly different data types Classes: group together variables and operations

3 Structures: struct  A struct is a collection of variables with possibly different types  The variables that form a struct are called fields or attributes  Structure is also known as record.  To define a structure use struct keyword  To access a filed of a structure use “. ”  The size of a structure is the sum of the sizes of the types of its fields  A structure can be the basic type of an array  A structure can be passed into functions (by value or by reference) as we did with basic types

4 Define struct & declare a struct variable  To define and use a struct, you can do one of the following struct Tag { typeX varNameX; struct Tag varNameStruct; typeY varNameY; … }; typedef struct Tag { typeX varNameX; TypeName varNameStruct; typeY varNameY; //or struct Tag varNameStruct; … } TypeName; typedef struct { typeX varNameX; TypeName varNameStruct; typeY varNameY; //or struct { … … } TypeName; } varNameStruct;

5 Initialize a struct TypeName varNameStruct={initValueForVarNameX, initValueForVarNameY,…}; Or varNameStruct.varNameX = initValueForVarNameX; varNameStruct.varNameY = initValueForVarNameY; …

6 Access and manipulate fields of a struct  Use “.” cout << varNameStruct.varNameX; cout << varNameStruct.varNameY; … varNameStruct.varNameX = aValue;

7 Example Write a program that computes the distance between 2D points.

8 Example  A 2D point has two coordinate. To represent it a point in 2D, we can use a struct as follows typedef struct { double x; double y; } Point2D;  A function that computes the distance between two 2D points is double distance (Point2D ptA, Point2D ptB) { return sqrt((ptA.x-ptB.x)*(ptA.x-ptB.x)+ (ptA.y-ptB.y)*(ptA.y-ptB.y); }  Calling the above function Point2D point1={1.0,2.0}, point2={3.0,5.5}; cout << distance (point1, point2) << endl; cin >> point1.x >> point1.y; cin >> point2.x >> point2.y; cout << distance (point1, point2) << endl;

9 Problem Write a program that models 3D points and the following operations on them: distance between two 3D points, and the symmetries of a point with respect to x axis, y axis, z axis, and the origin.

10 Motivation for classes  In real-life, objects do not only have attributes (or properties) but also perform operations (or we can perform operations on them) A person walks, talks, etc. An electronic component changes the voltage, the current, etc.  A class allows us to group (encapsulate) the attributes (as in structures) as well as operations/methods/functions.  In real-life, a system is a set of interoperating objects So, classes/objects allow us to write better programs  Easy to develop, understand, debug, and maintain.  Note that C++ structures allow us to write operations in them

11 Classes in C++  A class is a blueprint (type) from which objects are created Although there are many rabbits, they belong to the Rabbit class Although there are many students, they belong to the Student class  A class is like a factory for manufacturing objects  The attributes and the methods in a class can either be private or public Private elements are accessed only inside the class Public elements can be accessed outside the class The private concept is the main difference between a struct and class in C++  Everything in a struct is public!

12 specification for a class class ClassName { //attributes private: //or public TypeName1 attribute1; TypeName2 attribute2; … //methods public: ClassName(…); //constructor returnType1 methodName1(…);//methods returnType1 methodName1(…); … }; This part is usually done in.h file

13 Implementation of a class ClassName::ClassName(…) { … } returnType1 ClassName::methodName1(…){ … } returnType2 ClassName::methodName2(…){ … } This part is usually done in a.cpp file

14 Using a class  To create an object that belongs to ClassName ClassName varName=ClassName(arguments); //or ClassName varName(arguments);  To access the public elements of a class use “. ” varName.methodName1(arguments); varName.methodName2(arguments);  If an attribute is public, you can access it similarly to what we did for structures

15 Example class Student { private: string name; long studentID; public: Student(string,long); //constructor string getName(); long getID(); void study(); void print(); }; Student::Student(string n, long id){ name = n; studentID = id; } string Student::getName() { return name; } long Student::getID() { return studentID; } void Student::study() { cout << "Yes. I am studying hard."<<endl; } void Student::print() { cout << "My name is: "<< name <<". My student number is: "<<studentID<<endl; }

16 Example (Cont.) int main() { Student bel("Bel",123456);//object h.print(); h.study(); //I cannot do h.name. Why? return 0; }

17 Problem Write a program that computes the volume of a box, the areas of its sides, the translated box given a translation vector v.


Download ppt "Structures and Classes. Motivation  Types we discussed so far Basic types: char, int, float, double Arrays: a collection of indexed-variables with the."

Similar presentations


Ads by Google