0% found this document useful (0 votes)
15 views21 pages

Lecture 9

Uploaded by

Abdul Basit AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views21 pages

Lecture 9

Uploaded by

Abdul Basit AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Classes

Operation 1

Operation 2

Operation 3

Data

Data and operations bound in a single unit


Classes and Objects
struct StudentRec
{
String firstName;
String lastName;
Float gpa;
Int programGrade;
Int quizeGrade;
GradeType courseGrade;
};

A class is an expanded concept of a data structure: instead of


holding only data, it can hold both data and functions.
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.
Classes
Classes are generally declared using the keyword class,
with the following format:

class class_name
{
access_specifier_1: member1;
access_specifier_2: member2; ...
} object_names;

Access Specifier:

private members of a class are accessible only from within other members of the
same class or from their friends.

protected members are accessible from members of their same class and from their
friends, but also from members of their derived classes.

public members are accessible from anywhere where the object is visible.
Class and Objects
• In a class, all the functions that operate on the
data structure are grouped together in one
place along with the data
– Like a struct in C but with the addition of functions
• An Object is an instance of a class
• Objects share the same functions with other
objects of the same class, but each object or
instance has its own copy of the data structure
i.e, the member data
Time 1
Time 2

Set Function
Code Set Function
Code

Increment Function
Code Increment Function
Code

Write Function
Code Write Function
Code

Equal Function
Code Equal Function
Code

LessThan Function
Code LessThan Function
Code

hrs 5
hrs 7
mins 30
mins 55
secs 10
secs 2
Classes
Example:
Class Timetype
{
Public:
void Set(int, int, int);
void Increment();
void write () const;
bool Equal (Time) const;
bool LessThan (Time) const;
Private:
int hrs;
int min;
int secs;
};
Specification File
//Time.h
Class Time
{
Public:
void Set(int hours, int minutes, int seconds);
void Increment();
void write () const;
bool Equal (Time) const;
bool LessThan (Time) const;
Private:
int hrs;
int min;
int secs;
};
Implementation File
//Time.cpp
#include “Time.h”
#include <iostream>
using namespace std;

Void Time:: Set(int hours, int minutes, int seconds)

{
hrs=hours;
min=minutes;
secs=seconds;
}
//*************************
Implementation File
Void Time:: Increment()

{
secs++;
if (secs >59)
{
sec =0;
min++;
if (min >59)
{
mins=0;
hrs++;
if (hrs >23)
hrs =0;
}
}
}
Friend Functions
Example
Constructors
• Objects generally need to initialize variables or assign
dynamic memory during their process of creation to
become operative and to avoid returning unexpected
values during their execution. For example, what would
happen if in the previous example we called the member
function area() before having called function
set_values()? Probably we would have gotten an
undetermined result since the members x and y would
have never been assigned a value.
• In order to avoid that, a class can include a special function
called constructor, which is automatically called whenever
a new object of this class is created. This constructor
function must have the same name as the class, and
cannot have any return type; not even void.
Destructors
• The destructor fulfills the opposite functionality. It is
automatically called when an object is destroyed, either
because its scope of existence has finished (for example, if
it was defined as a local object within a function and the
function ends) or because it is an object dynamically
assigned and it is released using the operator delete.
• The destructor must have the same name as the class, but
preceded with a tilde sign (~) and it must also return no
value.
• The use of destructors is especially suitable when an object
assigns dynamic memory during its lifetime and at the
moment of being destroyed we want to release the memory
that the object was allocated.
class Point
{
public:
Point(); // parameterless default constructor
Point(int new_x, int new_y); // constructor with parameters
int getX();
int getY();
private:
int x;
int y;
};
...
Point::Point()
{ // default constructor
x = 0;
y = 0;
}
Point::Point(int new_x, int new_y) { // constructor
x = new_x;
y = new_y;
}
Overloading Constructors
Default constructor

//Correct

//Incorrect
Problem
Write code for the specification of a class Cube that calculates the area and volume based on the side
measurement.
It should have a default constructor that creates an object with 1.0 side length and a
parameterized constructor.
An observer that return the length of the side of the cube as float
An observer to return the area of the cube as double
An observer that returns the volume as a double.
An observer that displays the volume, the area, and the length of the side.
It also should have a transformer that sets the length of the side and resets to 1.0 if the program
gets a value 0 or less than 0
(Observers are functions that cannot make changes in class data)
Write the function definitions for the constructor, observers and transformer operations of the class
Cube given in part a.
Use the formulas
Area of the cube= 6 x side x side
Volume of the cube= side x side x side
Create an instance of Cube type in the main program and set its side length to -15.5 and call the
observer function to display the length of the side, volume and area of the cube. Again create a
parameterized instance with side length 34.6 and display the characteristics of the cube.

You might also like