Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)

Similar presentations


Presentation on theme: "Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)"— Presentation transcript:

1 Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)

2 Classes in C++ A class is a means of abstraction in C++ A class is a specification of a group of objects that all have the same essential properties A variable of class type is known as object and the operations on that type are called methods

3 Difference b/w Structures and Classes A structure is a collection of named fields whereas a class is a collection of named fields and methods that apply to objects of that class type. Structures do not support data encapsulation whereas classes do, restricting access to members of a class to methods of a class itself FOR EXAMPLE...

4 A class example class Student Properties (data members) name, graduation year, list of courses, number of courses Operations (methods) List Courses List Student Information Add a Course Set Graduation Date

5 A C++ definition of a class class Student// declares a class data type {// does not allocate memory private :// 4 private data members char studentName[30]; string courses[6]; int numCourses; int gradDate; public : // 5 public function members void AddCourse(string9 CourseName); void ListCourses(void) const; void ListInfo(void) const; void SetGradDate(int year); Student(); } ; // Constructor Function

6 An object is an instance of a class For example: object studentA Name: Andrea Student Graduation Year: 2007 List of Courses: CSCI132, MATH331 Number of courses: 5

7 Use of C++ data type class Facilitates re-use of C++ code for an ADT. Software that uses the class is called a client. Variables of the class type are called objects or instances of the class. Client code uses public member functions to handle its class objects. Private members cannot be directly accessed by client code. It can only be accessed by member functions.

8 C++ Data Type class represents an ADT 2 kinds of class members: data members and function members class members are private by default data members are generally private function members are generally declared public private class members can be accessed only by the class member functions, not by client code.

9 Implementing Methods Methods of a class can be implemented within the declaration of a class or outside it.

10 Example 1 class Student// declares a class data type {// does not allocate memory private :// 4 private data members char studentName[30]; string courses[6]; int numCourses; int gradDate; public : Student(); // 5 public function members void AddCourse(string CourseName); void ListCourses(void); void ListInfo(void); void SetGradDate(int year) { gradDate = year; } } ;

11 Example 2 // SPECIFICATION FILE ( student.h ) // Specifies the data and function members. class Student { public:... private:... } ; // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions....

12 Implementation file for Student // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions. #include "student.h" // also must appear in client code #include... void Student :: SetGradDate (int year ) { gradDate = year; }...

13 Implementing Methods Student :: SetGradDate(int year); { gradDate = year; } In the heading, the function's name is preceded by the class name and :: - otherwise C++ won't realize this is a class’s member function. Within the body of the function, the class’s member variables and other member functions may all be accessed.

14 #include "student.h" // includes specification of the class #include int main ( void ) { int newYear; string newCourse; Student newStudent;// create new Student object cout << "Enter graduation year: "; cin >> newYear; newStudent.SetGradDate(newYear); // set Graduation date cout << "Enter course to be added: "; cin >> newCourse; newStudent.AddCourse(newCourse); // Add a course newStudent.ListInfo(); // List Student information newStudent.ListCourses(); // List courses } // end of main Client Code Using Student

15 Inheritance Inheritance: The ability of a class to derive properties from a previously defined class. Inheritance is a relationship among classes.   A class can derive the behavior and structure of another previously defined class. base class derived class   The derived class is also called as subclass, child.   The base class is also called as superclass, parent.

16 Inheritance cont. In C++, a derived class inherits all the members of its base class, except the constructors and destructor.   A derived class has data members and member functions of the base class in addition to the members it defines.   A derived class can revise any inherited member function.

17 Constructor Arguments When a constructor does not have to accept arguments, it is called an object’s default constructor. Like regular functions, constructors may accept arguments, have default arguments, be declared inline, and be overloaded.

18 Destructors A destructor is a member function that is automatically called when an object is destroyed.   Destructors have the same name as the class, preceded by a tilde character (~)   In the same way that a constructor is called then the object is created, the destructor is automatically called when the object is destroyed.   In the same way that a constructor sets things up when an object is created, a destructor performs shutdown procedures when an object is destroyed.

19 Function Overloading In C++, several functions of the same name can be defined as long as these function name different sets of parameters (different types or different number of parameters).

20 Object Orientation In recent years, the object oriented way of doing things has become increasingly popular. Object oriented ways of thinking have been applied in many different areas of computer science. E.g.   Software system design   Operating systems   Programming languages

21 O-O design principles Organise software as a collection of objects and classes. Objects provide a set of operations (methods- behaviour) Objects have an internal state. Objects belong to (are instances of) a class.

22 Example: class Rectangle { private: float width, length, area; public: void setData(float, float); void calcArea(void); float getWidth(void); float getLength(void); float getArea(void); };

23 13.3 Defining Member Functions Class member functions are defined similarly to regular functions. void Rectangle::setData(float w, float l) { width = w; length = l; }

24 13.4 Defining an Instance of a Class Class objects must be defined after the class is declared. Defining a class object is called the instantiation of a class. Rectangle box; // box is an instance of Rectangle

25 Accessing an Object’s Members box.calcArea();

26 Pointers to Objects Rectangle *boxPtr; boxPtr = &box; boxPtr->setData(15,12);


Download ppt "Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)"

Similar presentations


Ads by Google