Hardware: EEPROM
- Electrically Erasable Programmable Read-Only Memory
- non-volatile memory
- Why Use EEPROM
- Situations where data unique to situation needs a more permanent home
- What kind of data can be stored
- Anything that can be represented as bytes
- One byte=8 bits
Software
- In C++, classes are building blocks of object-oriented programming
- Similar to structure, except designed to also include function members
- Instances of a class are called objects
Defining a Class Data Type
Class definition consists of two parts: class declaration and class implementation
Class declaration: name of class is specified using the keyword "class," body of declaration consists of type declaration statements for the data members and function prototypes for function members
Example 1.1
class sample
{
public:
void print();
double variable();
};
Class design includes function members to implement desired operations.
Example 1.2
// These statements define a class for xy-coordinates.
// This declaration is stored in xy_coordinate.h.
#include <iostream>
Public: May be referenced anywhere in user program
Private: May only be referenced by member functions of class. Private member functions can only be called by other member functions.
Class Implementation: Consists of all member function definitions. When defining member function, use :: in function definition between class name and function name to specify that function is member of class.
Example 2.1
Code for xy_coordinate.h header file
// These statements define a class for xy-coordinates.
// This declaration is stored in xy_coordinate.h.
#include <iostream>
#include <cmath>
using namespace std;
class xy_coordinate
{
// Declare function prototypes for public members.
public:
void input();
void print();
double radius();
double angle();
private: // Declare private data members.
double x, y;
};
// These statements define implementation of an
// xy_coordinate class. They are stored in xy-coordinate.h.
// This function reads the xy coordinates from the keyboard.
void xy_coordinate::input()
{
cin >> x >> y;
}
// This function prints the xy coordinates to the screen.
void xy_coordinate::print()
{
cout << "(" << x << "," << y << ")" << "\n";
}
// This function computes the radius.
double xy_coordinate::radius()
{
return sqrt(x*x + y*y);
}
// This function computes the angle in radians.
double xy_coordinate::angle()
{
// Compute angle of polar form.
double z, pi=3.141593;
if (x >= 0)
z = atan(y/x);
if (x<0 && y>0)
z = atan(y/x) + pi;
if (x<0 && y<=0)
z = atan(y/x) - pi;
if (x==0 && y==0)
z = 0;
return z;
}
Code for using class and its functions
// This program demonstrates the use of
// the xy_coordinate class and its functions.
#include <iostream>
#include <cmath>
#include "xy_coordinate.h"
using namespace std;
int main(void)
{
// Declare and initialize variables.
xy_coordinate pt1;
// Read input point.
cout << "Enter x and y coordinates:" << endl;
pt1.input();
// Print coordinate in xy form and polar form.
cout.setf(ios::fixed);
cout.precision(2);
cout << "Coordinate in xy form:" << endl;
pt1.print();
cout << "Coordinate in polar form:" << endl;
cout << "magnitude: " << pt1.radius() << endl;
cout << "phase (in degrees)" << pt1.angle()*180/3.141593 << endl;
// Exit program.
system("pause");
return 0;
}
The system pause is not necessarily needed, however, after having issues earlier with Dev C++, I decided to add it in anyway after David Pan showed me how to use it. I tried it first with the system pause, then commented out the system pause and ran the program again, which still worked.
Constructor Functions
When defining variable, they are often assigned initial values. If the variable is not initialized when defined, it holds an unknown value until valid data is assigned.
Constructor Functions: Special member functions that are called automatically when object of that class is declared, used to initialize data members of object being defined.
3 Unique Properties of Constructor Functions
- Called automatically when object of that class is declared
- Name of constructor function is name of the class
- No return value associated with constructor function, and is not void function
Homework
Programming
Main Function:
//Day 26 Homework
//Date entry using class functions
//This program allows the user to enter the date using date input functions
//and prints the data in each of the defined forms using class member functions
#include <iostream>
#include <cmath>
#include "dateInputHomework.h"
using namespace std;
int main(void)
{
//Declare and initialize variables
dateInput myDate;
//Read input
myDate.input();
//Print date
myDate.printNum();
myDate.printText();
system("pause");
return 0;
}
Header File with Classes:
//These statements define the implementation of the dateInput
//class. They are stored in dateInputHomework.h
#include <iostream>
#include <cmath>
using namespace std;
class dateInput
{
//Function prototypes for public members
public:
void input();
void printNum();
void printText();
dateInput();
dateInput(double a, double b, double c);
//Private data member declaration
private:
int month, day, year;
};
//This function takes a date input and checks the validity of the date
//provided
void dateInput::input()
{
cout << "Input month, day, and year" << "\n";
cin >> month >> day >> year;
//Checks if month is within range and requests new month input
//if it is not
while(month<1 || month>12)
{
cout << "Invalid month. Please re-input month" << "\n";
cin >> month;
}
//Checks if date is within range and requests new day input
//if it is not
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
{
while(day<1 || day>31)
{
cout << "Invalid day for inputted month. Please enter day between 1 and 31" << "\n";
cin >> day;
}
}
//Checks if date is within range and requests new day input
//if it is not
if(month==4 || month==6 || month==9 || month==11)
{
while(day<1 || day>30)
{
cout << "Invalid day for inputted month. Please enter day between 1 and 30" << "\n";
cin >> day;
}
}
//year%4==0 indicates a leap year. This section then checks if
//the day provided is within the range, and requests
//a new value if it is not
if(month==2 && year%4==0)
{
while(day<1 || day>29)
{
cout << "Invalid day for inputted month. Please enter day between 1 and 29" << "\n";
}
}
//year%4!=0 indicates a non-leap year, which means that
//the dates must be between 1 and 28
if(month==2 && year%4!=0)
{
while(day<1 || day>28)
{
cout << "Invalid day for inputted month. Please enter day between 1 and 28" << "\n";
cin >> day;
}
}
}
//This function prints the date in mm/dd/yyyy format
void dateInput::printNum()
{
cout << month << "/" << day << "/" << year << "\n";
}
//This function prints the date in Month Day, Year format
void dateInput::printText()
{
switch(month)
{
case 1 :
cout << "January " << day << ", " << year << "\n";
break;
case 2 :
cout << "February " << day << ", " << year << "\n";
break;
case 3 :
cout << "March " << day << ", " << year << "\n";
break;
case 4 :
cout << "April " << day << ", " << year << "\n";
break;
case 5 :
cout << "May " << day << ", " << year << "\n";
break;
case 6 :
cout << "June " << day << ", " << year << "\n";
break;
case 7 :
cout << "July " << day << ", " << year << "\n";
break;
case 8 :
cout << "August " << day << ", " << year << "\n";
break;
case 9 :
cout << "September " << day << ", " << year << "\n";
break;
case 10 :
cout << "October " << day << ", " << year << "\n";
break;
case 11 :
cout << "November " << day << ", " << year << "\n";
break;
case 12 :
cout << "December " << day << ", " << year << "\n";
break;
}
}
//This constructor function initializes all values to 0
dateInput::dateInput()
{
month=0;
day=0;
year=0;
}
//This constructor function initializes values to parameters
dateInput::dateInput(double a, double b, double c)
{
month=a;
day=b;
year=c;
}
Comments
Post a Comment