classesI - EECE 230 Lecture Notes: S. Karaki 17 6 6.1...

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

View Full Document Right Arrow Icon
EECE 230 – Lecture Notes: S. Karaki 17 6 User-Defined Data Types: Classes 6.1 Introduction C++ allows construction and manipulation of data items that mirror real world categories. Basic or primitive data types are built in classes compared to user defined data types. Atomic objects refer to primitive data types and compound data types are compound objects. 6.5 Class Definition Let’s create a class called box that has a height, width and length: class box { public: double height , width, length; }; Notes: class tells compiler that a class is defined box name of the class { marks the start of class body double declaring member variables of the class } marks the end of the body ; end of class definition height, width and length are member variables Now we can introduce an object of type box using the same syntax of a usual declaration: box x; // Declare an object x of type box. 6.6 Accessing Member Variables Access member variables of x by using a class member operator .” or “->”as follows: // Access using the “.” operator box x; x . height= 10; // Set the height of x x . length= 4; // Set the length of x // Access using the “->” operator box *xPtr; xPtr->width= 5; // Set the width of x Write a program to define a box, assign values to member variables and compute the box volume.
Background image of page 1

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

View Full DocumentRight Arrow Icon
EECE 230 – Lecture Notes: S. Karaki 18 #include <iostream> using std::cout; using std::endl; class box { public: double height, width, length; }; double volume(double h, double w, double l) { return (h w l); } void main( ) { box x; x.height = 10; x.width = 5; x.length = 4; cout << “The volume of the box is ” << volume(x.height, x.width, x.length)<<endl; } Output: The volume of the box is 200 The semicolon at end allows the definition of global variables (x1, x2) as follows: class box { public: double height, width, length; } x1, x2; Semicolon indicates the end of any defined variable list. Define an alternative function volume with one argument of type box: double volume(box b) { return (b.height * b.width * b.length); } Then in the program volume is calculated as follows: cout << … <<volume(x) << endl; More on Function Overloading To deal with new class tank, we need to add another class definition and volume calculation: #include <iostream> using std::cout; using std::endl;
Background image of page 2
EECE 230 – Lecture Notes: S. Karaki 19 const double pi = 3.14159; class box { public: double height, width, length; }; class tank { public: double radius, length; }; double volume(box b) { return (b.height * b.width * b.length); } double volume (tank t) { return(pi * t.radius * t.radius * t.length);} void main ( ) { box x; x.height = 10; x.width = 5; x.length = 4; tank y; y.radius = 3; y.length = 4; cout << “The volume of the box is ” << volume(x) <<endl; << “The volume of the tank is ” << volume(y) <<endl; } Output: The volume of the box is 200 The volume of the tank is 113.09734 If more than one definition for the function ‘volume’ then it is said to be overloaded
Background image of page 3

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

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

This note was uploaded on 02/26/2010 for the course EECE 230 taught by Professor Samikaraki during the Spring '07 term at American University of Beirut.

Page1 / 12

classesI - EECE 230 Lecture Notes: S. Karaki 17 6 6.1...

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

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