Topic 2 Introduction to Classes
Topic 2 Introduction to Classes
Computer Applications-2
Introduction to Classes
Procedural and Object-Oriented
Programming
• Procedural programming focuses on the process/actions
that occur in a program
Public Members
More on Access Specifiers
• Can be listed in any order in a class
int Rectangle::setWidth(double w)
{
width = w;
}
Accessors and Mutators
• Mutator: a member function that stores a value in a private
member variable, or changes its value in some way
Rectangle(double, double);
• Use parameters in the definition:
rect area: 12
rectb area: 30
More About Default Constructors
• If all of a constructor's parameters have default arguments,
then it is a default constructor. For example:
class Rectangle {
int width, height;
public:
Rectangle ();
Rectangle (int,int);
int area (void) {return (width*height);}
};
Overloading Constructors
Rectangle::Rectangle () {
width = 5;
height = 5;
}
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
Overloading Constructors
int main () {
Rectangle r1(3,4);
Rectangle r2;
cout << "r1 area: " << r1.area() << endl;
cout << "r2 area: " << r2.area() << endl;
return 0;
}
rect area: 12
rectb area: 25
Only One Default Constructor
and One Destructor
• Do not provide more than one default constructor for a class:
one that takes no arguments and one that has default
arguments for all parameters
Square();
Square(int = 0); // will not compile
- width : double
- length : double
UML Parameter Type Notation
+ setWidth(w : double)
UML Function Return Type Notation
• To indicate the data type of a function’s return value, place a
colon followed by the name of the data type after the function’s
parameter list.