// Filename: DATECHK.CPP
// Demonstrates a complete OOP program with class data
// Add simply error-checking to the member function code
// so that reasonable values are assigned to the date.
#include <iostream.h>
class Date {
int day;
int month;
int year;
public: // Everything that follows is accessible by main()
void prDate(); // Member functions
void setDay(int d);
void setMonth(int m);
void setYear(int y);
};
void Date::prDate() // :: Tells C++ the owner class
{
cout << "Date: " << day << "/" << month << "/"
<< year << "" << endl;
}
void Date::setDay(int d)
{
if (d <= 31)
{ day = d; }
else
{ cout << endl << "The day cannot be " << d << "!" << endl; }
}
void Date::setMonth(int m)
{
if (m <= 12)
{ month = m; }
else
{ cout << endl << "The month cannot be " << m << "!" << endl; }
}
void Date::setYear(int y)
{
if (y >= 90)
{ year = y; }
else
{ cout << endl << "The year cannot be " << y << "!" << endl; }
}
void main()
{
Date today; // Define a new active class variable
// today.day = 20 is NOT possible!!! You must initialize
// the data can only be initialized through member functions
today.setDay(20); // Initialize the day
today.setMonth(6); // Initialize the month
today.setYear(1994); // Initialize the year
today.prDate(); // Print the date (You could'nt print the
// date's values via the dot operator!)
return;
}
=========================
// Filename: DEFPNTS.CPP
// Defines several variables and pointers to those variables
#include <iostream.h>
void main()
{
int i1 = 14; // Define and initialize an integer
int* ip1; // Define a pointer to an integer
int i2 = 20;
int* ip2 = &i2; // Define and initialize the pointer
float f = 92.345;
float* fp = &f;
double d;
double* dp;
ip1 = &i1;
cout.precision(3);
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout << "i1 is " << i1 << " and *ip1 is also "
<< *ip1 << endl;
cout << "i2 is " << i2 << " and *ip2 is also "
<< *ip2 << endl;
cout << "f is " << f << " and *fp is also "
<< *fp << endl;
*fp = 1010.10;
cout << "After changing f through fp, " << endl;
cout << "f is now " << f << " and *fp is also "
<< *fp << endl;
dp = &d;
*dp = 83949443.54333; // Change dp
cout << "d is now " << d << " and *dp is also "
<< *dp << endl;
return;
}
========================
// Filename: DISSECT.CPP
// Simple program for you to analyze
#include <iostream.h>
void main()
{
int age, weight; // These lines define four variables
char initial;
double salary;
age = 13; // Assigns an integer literal to age
weight = age * 6; // Assigns a weight based on a formula
initial = 'D'; // All character literals are
// enclosed in single quotes
salary = 200.50; // salary requires a floating-point
// value because it was defined to
// be a floating-point
salary = salary * 2.5; // Changes what was in salary
// Next line sends the values of the variables to the screen
cout << age << ", " << weight << ", " << initial
<< ", " << salary;
}
===============================
// Filename: DOUSRAGE.CPP
// Uses a do-while loop to get an age from the user
#include <iostream.h>
void main()
{
int age;
// In the previous listing, two extra lines went here
do
{ cout << "How old are you? "; // Get the age
cin >> age;
if ((age < 5) || (age > 110))
{ cout << "I'm not sure that I believe you are "
<< age << endl;
cout << "Try again..." << endl << endl; }
} while ((age < 5) || (age > 110)); // Quit after good input
// The program continues if the user entered a reasonable age
if (age < 16)
{ cout << "Your curfew is at 11:30 pm" << endl;
cout << "Have fun!" << endl;
}
else
{
cout << "You're old enough to drive, so be careful ";
cout << "out there!" << endl;
}
return;
}
==============================
Tidak ada komentar:
Posting Komentar