lect9_1 - Programmer Defined Types Classes and Objects 1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Programmer Defined Types - Classes and Objects 1 Programmer Defined Types - Classes and Objects Object oriented programming (OOP) revolves around the notion of an object . Objects are created using programmer defined types, called classes . In C++, a class is the same thing as type. Classes are more advanced types than the basic types such as int , double , and array types. For the basic types, the C++ language provides a number of predefined operations; for example, integers can be added, an array element can be accessed using the subscript operator. In the case of programmer defined classes, it is left entirely to the programmer to decide what operations are to be used and what types of data are to be handled. A type that defines a collection of data items and a set of operations on the data items, is called an Abstract Data Type (ADT). The C++ classes can be viewed as implementations of the ADTs. A class defines a type that includes data specification and functions operating on the data. Functions defined in the class are called member functions (or methods ) and the data variables used in the class are called data members . This binding of the data and related methods is called data encapsulation . An object is an instance of a class and the class provides a blueprint for the construc- tion of objects of that class. Defining a class A class can be used to bundle several data items together into a single variable. Suppose that we want to do some computation involving rational numbers. Recall that a rational number is any number that can be written as p/q , where p and q are integers and q != 0 . Note that every integer is also a rational number, since any integer x can be expressed as x/1 . For example, 7/1 ,-8/3 , and 0/2 are rational numbers. We want to define a class that deals with rational numbers. Instead of presenting the full definition of the class in its final form, we will define it incrementally, focussing on one topic at a time. Programmer Defined Types - Classes and Objects 2 The following is our first version of the class definition for rational numbers. class RationalNum { private: // data members int numer; int denom; public: // member functions void setRationalNum(int n, int d); // sets numer = n, denom = d void outputRationalNum(); // outputs numer and denom int getNumer(); // returns numer int getDenom(); // returns denom }; // end of class RationalNum (NOTICE THE SEMICOLON AT THE END) A class definition always begins with the keyword class , After the keyword class , the name of the class is specified. In our example, the name of the class is RationalNum . A class definition is usually placed at the beginning of a program, where the globally defined constant declarations are placed. The class type is then available to the functions that follow the class definition....
View Full Document

Page1 / 9

lect9_1 - Programmer Defined Types Classes and Objects 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online