Time::Time(int newHours, int newMinutes, int newSecs)
{
setTime(newHours, newMinutes, newSecs);
}
Create an array that store empId, name and salary.
struct empRec
{
int empID,
string name,
double salary,
};
empRec staff[10];
structure.member
member access operator
dot operator
What is a class
Class: programmer-defined data typeAlso called an abstract data typeCombination of data and their associated operations (capabilities)
Class components
Two components:
Declaration section: declares data types and function for class
Implementation section: defines functions whose prototypes were declared in declaration section
Class members:
Class members:
Data members (instance variables)
Member functions
class construction
// declaration section
class className
{
data declarations
function prototypes
};
// implementation section
function definitions
why private
private:
class members can only be accessed using class functions
Enforces data security (data hiding)
Constructor method:
initializes class data members with values
Constructor function has same name as the class
Constructor function has no return type (a requirement for this special function)
class Date
{
private:
int month, day, year;
public:
Date(int =7, int=4, int=2012);
void setDate(int, int, int);
void showDate(void);
};
Write the constructor.
Date::Date(int mm, int dd, int yyyy)
{
month=mm;
day=dd;
year=yyyy;
}
member function format
returnType className::methdoName(parameter list)
{
method body
}
class Date
{
private:
int month, day, year;
public:
Date(int =7, int=4, int=2012);
void setDate(int, int, int);
void showDate(void);};
Write setDate.
void Date::setDate(int mm, int dd, int yyyy)
{
month=mm;
day=dd;
year=yyyy;
}
class Date
{
private:
int month, day, year;
public:
Date(int =7, int=4, int=2012);
void setDate(int, int, int);
void showDate(void);};
Write showDate.
void Date::showDate()
{
cout <<"The date is " << month << "/" << day <<"/" << year;
}
::
scope resolution operator
to identify function as a member of the class
class Date
{
private:
int month, day, year;
public:
Date(int =7, int=4, int=2012);
void setDate(int, int, int);
void showDate(void);
};
Date::Date(int mm, int dd, int yyyy)
{ month=mm; day=dd; year=yyyy;}
1. Declare a and b with default values.
2. Declare c with 4,1,2000
3.show all 3 dates
int main()
{
Date a, b, c(4,1,2000);
b.setDate(12,25,2009);
a.showDate();
b.showDate();
c.showDate();
}
class Date
{
private:
int month, day, year;
public:
Date(int =7, int=4, int=2012);
void setDate(int, int, int);
void showDate(void);
};
int main()
{
Date a, b, c(4,1,2000);
b.month;
}
Is the last line of main valid?
No
The data member of Date are private
month is provide
need to use b.showDate();
Instantiation
Instantiation: process of creating a new object
Creates new set of data members belonging to new object: determines the object’s state
What is the purpose of a constructor?
When is it used?
initialize a new object’s data members
Constructors are called whenever an object is created
Date c(4,1,2009);
(Other methods must be called explicitly by name)
A class declaration must be terminated with a semicolon.
T or F
T
A constructor does not need a return type.
T/F
T
In the implementation section, the member method needs the class name and scope resolution operator. T/F
T
What is a constructor?
1. A constructor is a special function that initializes the data members of a class. It has the same name as the class.
What is an ADT? Why do we use it for our classes?
2. An ADT=Abstract Data Type.
We use it to separate the “what” from the “how”. The specification file (“what”) lists the data members and function prototypes and the implementation file (“how”) is the code for the member functions. Only the specification file need be viewed, the implementation file is not viewable, ensuring information hiding,
When is a constructor called?
3. Constructors are called automatically when an instance of the class is created
A variable of class types is called
object
In C++, a class constructor without a parameter list is called a(n)
default constructor.
7.Distinguish between an object and a class.
7. A class defines what an object can do, an object is an instance of the class.
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types.
What is the default access for a class? For a struct?
8. Default access for a class is private, default access for a struct is public.
class uses access operator (.)
By default, all members of a class declared with the class keyword have private access for all its members.
9. What is meant by the term encapsulation?
9. All of the members of a class are defined in a class and can only be accessed via an instance of the class. Private members can only be accessed by other members of the class.
10.What is class scope?
10. All function members of a class can access all the data members. These data members do not have to passed or returned from a member function.
What does the reserved word private indicate within a class?
11. Private members can only be access by other members in the class. The client code cannot access any private members.
When should you code const?
12. Code const on member functions when you do not want data members to be changed.