Академический Документы
Профессиональный Документы
Культура Документы
Object-Oriented
Programming
Classes 3
Lecture 6
Contents:
Objects and Data Types
Objects within Classes
Dynamically Allocation of Objects
Pointers to Class Objects
Arrays of Objects
Arrays of Pointers to Objects
Dynamically Allocation in Classes
About Structures
Unions
Pointers to Structures
Objects and Data Types
The key thing to remember about objects are
simply user-defined data types.
MyClass test;
MyClass test2(5, 6);
#include <iostream> int main( )
using namespace std; {
class Car { Car c;
public:
void drive ( int speed, int distance); c.drive(100, 2000);
void displayData ( ); c.displayData();
private:
int speed; system("pause");
int distance; return 0;
}; }
CRectangle::CRectangle () {
width = 5;
height = 5;
}
class MyClass {
…
private:
MyOtherClass x; // Instance of a class
MyOtherClass *y; // Pointer to an instance of
// a class
};
Dynamically Allocation of Objects
struct PayRoll {
int empNumber;
string name;
double hours,
payRate,
grossPay;
};
Initialization of Structures
1. Using an Initialization List
struct Date
{ int day, month, year;
};
struct Date
Date( ) // Constructor
{ int day = 23,
{ day = 23;
month = 8,
month = 8;
year = 1983;
year = 1983;
};
}
};
Working with Structures
struct Time
{ int hours, minutes, seconds;
};
Time t, n;
t.hours = 5;
t.minutes = 20;
t.seconds = 15;
n.hours = 0;
PayRoll *employee;
employee = new PayRoll;
employee -> empNumber = 001;
( * employee ) . empNumber = 001;
Unions
union PaySource
{
short hours;
float sales;
};
PaySource employee;
Discussion:
Design a class for a widget manufacturing plant.
Assuming that 10 widgets may be produced
each hour, the class object will calculate how
many days it will take to produce any number of
widgets. (The plant operates two shifts of eight
hours each per day.) Write a program that asks
the user for the number of widgets that have
been ordered and then displays the number of
days it will take to produce them.