Lesson 6 Classes: Objectives
Lesson 6 Classes: Objectives
Classes
Objectives
Cat properties
Can eat
Can sleep
Declaring a class
Syntax:
class class_name {
permission_label_1:
member1a;
member1b;
permission_label_2:
member2a;
member2b;
...
};
where class_name is a name for the class (user defined type). The body of the
declaration can contain members, that can be either data or function
declarations, and optionally permission labels, that can be any of these three
keywords: private:, public: or protected:. They make reference to the permission
which the following members acquire:
Member names
Example: char name
To declare as class cat: char breed
int age
class Cat{
public:
void sleep ();
Member methods
void eat ();
private: void eat
char name, breed; void sleep
int age;
}; Figure 4. class Cat
Syntax:
Class_name class_identifier; Cat properties
garfield.name
Example garfield.breed
Cat garfield; garfield.age
Cat properties
garfield.eat()
garfield.sleep()
The public members (variables and functions) of the class can be manipulated
using the Dot Operator.
Example:
garfield.age = 4;
garfield.sleep();
The operator :: is called the scope resolution operator and serves a purpose
similar to that of the dot operator. Both the dot operator and the scope
resolution operator are used to tell what a member function is a member of.
However the scope resolution operator :: is used with a class name.
Syntax:
Example:
void Cat::sleep( )
{
cout<< “ zzz. Zzzz.. zzz”;
}
Accessor is function member that allows to read data and usually include the
word get for function name, while Mutator is a function that allows to change
the data and usually include the word set for function name.
Example:
int Cat::get_weight( )
{ ….}
class Person
{
public:
void SetAge(int Age); // Set the age
void SetName(char* chrName); // Set the name
char* GetName(); // get the name
{
pName = chrName;
}
char* Person::GetName()
{
return pName;
}
int Person::GetAge()
{
return pAge;
}
void Person::Greeting1()
{
cout << "Hello!\n";
}
void Person::SayAge()
{
// Check Person age
if (pAge < 2)
// Person is 1 or less years old
cout << "I am a baby!\n";
else
// Person is over 1 years old
cout << "I am " << pAge << " years old!\n";
}
Listing 3. LabExample3.cpp – Sample Class
void Person::SayName()
{
cout << "My name is " << pName << "!\n";
}
void Person::Goodbye()
{
cout << "Good-bye!!\n";
}
int main()
{
// Define our perosn
Person piper;
// Set up piper's age
piper.SetAge(13);
// Give piper's full name
piper.SetName("Piper Halliwell");
A destructor cleans up after your object and frees any memory you might have
allocated. It always has the same name as your class preceded by a tiled (~).
It has an empty function body; that is its take no action.
All objects must be constructed and destructed. Your compiler creates the
default constructor and destructor which is an empty method.
int Cat::GetAge()
{
return itsAge;
}
void Meow()
{
cout<<"meow..";
}
int main()
{
Cat Frisky(5);
cout<<"Firsky is a cat who is"
<<Frisky.GetAge()<<" years old."<<endl;
cout<<"Next year, Frisky will be ";
Frisky.SetAge(6);
cout<<Frisky.GetAge()
<<" years old."<<endl;
return 0;
}
Const Member Functions
Declaring a member function to be const means that you are promising that the
method will not change the value of any of the members of the class. Attach
the const keyword at the end of the function header.
The interface or the class declaration tells the compiler what the class is, what
data it holds, and what functions it has. It also tells the user how to interact
with the class. Interfaces are usually stored in a .h file.
The implementation of the function definition tells how the function works.
Implementations are stored in the .cpp file.
Inline Implementation
class Cat
{
public:
Cat (int initialAge);
~Cat();
int GetAge() const {return itsAge;} //inline
void SetAge (int age) { itsAge=age;}//inline
void Meow() const { cout<<"Meow";} //const method
private:
int itsAge;
};
Cat::~Cat()
{
}
Listing 5B. LabExample5.cpp - Cat implementation
int main()
{
Cat Frisky(5);
Frisky.Meow();
cout<<"Frisky is a cat who is "
<<Frisky.GetAge()
<<" years old";
Frisky.Meow();
Frisky.SetAge(7);
cout<<"\n Now Frisky is "
<<Frisky.GetAge()
<<" years old.\n";
return 0;
}
C++ allows definition of more than one class in a file and immediately use this
classes within the same implementation.
class Point
{
public:
void SetX(int x) { itsX=x;}
void SetY(int y) { itsY=y;}
int GetX() const {return itsX;}
int GetY() const {return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle(int top, int bottom, int left, int right);
~Rectangle();
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsBottom;
int itsLeft;
int itsRight;
};
itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);
itsUpperRight.SetX(right);
itsUpperRight.SetY(top);
itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);
itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}
int main()
{
Rectangle MyRectangle(100,20,50,80);
cout<<"Area: "<<Area;
cout<<"\n UpperLeft X Coordinate: "
<<MyRectangle.GetUpperLeft().GetX() << endl;
return 0;
}
CRectangle::get_area( );
int Crectangle::x = 0;
int CRectangle::height=10;
int CRectangle::x = 0;
int CRectangle::y = 0;
Listing 7. LabExample7.cpp. Static member functions
void main()
{
int area, volume, a, b;
CRectangle rect, square;
rect.set_values(a,b);
area=CRectangle::get_area();
volume=CRectangle::get_volume();
square.set_values(a,b);
area=CRectangle::get_area();
volume=CRectangle::get_volume();
int CRectangle::get_area ()
{
return compute_area();
}
int CRectangle::get_volume ()
{
return compute_volume();
}
int CRectangle::compute_area()
{
return (x*y);
}
int CRectangle::compute_volume()
{
return (compute_area() * height);
}
Arrays of Objects
Objects can also be represented in arrays. See Listing 7 for example.
Exercise # Date
Name Course/Section
Problem Statement:
Design a class that can be used to represent types of food. A type of food is classified as
basic or prepared. Basic foods are further classified as Dairy, Meat, Fruit, Vegetable, or
Grain. The services provided by the class include the ability to enter data for new food,
change data for new food, display existing data for new food. Create a program that will
allow the user to enter data for four food items and display the entered data. Provide a
menu for selection of services and allow user input for program rerun and/or termination.
Criteria Score
Total
Checked by:
Instructor