0% found this document useful (0 votes)
36 views13 pages

Class Member Functions and Instantiation

Uploaded by

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

Class Member Functions and Instantiation

Uploaded by

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

•Selecting member functions for your class

One of the most demanding task in building a


class is defining its interface. i.e. identifying the
operations needed by the objects of the class and
making them as either public or private member
functions. This is because you need to think
about all the operations in the program.
One approach is to use is to create a list of all
possible operations and go ahead and declare
these operations as member functions. The
member functions are similar to functions in
structured programming only that they appear
within a class.
•Instantiation of objects
•Once you create an object that belongs
to a class, you may be interested in
giving it data values. We do this by
calling upon, methods that can be used to
get or to set the data values.
•This method of giving values to a
particular object is called instantiation.
•For example
•Model a class template with
appropriate member functions that
can be used to instantiate the
following values to their respective
data.
•23 model
•6706 part
•550.90 fuel price
•#include <iostream>
•using namespace std;
•class spare_parts
•{
•private:
• int model,part;
• double fuel_price;
•public:
• void set_part(intmodel_no,int parts, double cost)
• { model=model_no;
• part=parts;
• fuel_price=cost;
• }
Void display_parts()// member function display_parts()
{
cout<<"model no ="<<model<<endl;
cout<<"part no ="<<part<<endl;
cout<<"mfuel price ="<<fuel_price<<endl;
}
};
int main()
spare_parts my_car_spare;
my_car_spare.set_part(23,6706,550.90);
my_car_spare.display_parts();
return 0;
}
Interface and implementation.

•An accessor function provides a public interface to


the private member data of the class.
•Each a ccessor function must have an
implementation.
•The implementation is called function definition;
•A member function defined outside the class must
begin with the name of the class followed by two
colons, the name of the function and its
parameters.
•For example, if we have a class called employee within which
there is a public member function called get_data(which is its
interface), to define the function reference using the interface,
use
Employee::get_data()
resolution operator
{
Statements
}
•The functions are defined almost in the same way
we defined functions in structured programming
only that the function must be qualified using the
class name followed by two colons.
Example
•Define member functions get data and display outside a class. The
member functions should be able to:
a) get_datamember function should be able to:
Capture the following details
 The employee ID
 The name of the employee
 The employee’s department
 His/Her basic salary
 His/Her age
a) display member function should be able to:
display the following details
 Persoonal Number(ID NO)
 Department
 Salary
 age
•#include <iostream.h>
•#include<string.h>
•#include<iomanip.h>
•constint MAX=30;
•class employee
•{
•private:
• intemployeeID,age;
• char name[MAX];
• char department[MAX];
• double salary;
•public:
• void getdata(void);
• void display(void);
•};
•void employee::getdata(void)
•{
• cout<<"Enter employee ID"<<endl;
• cin>>employeeID;
• cout<<"Enter name"<<endl;
• cin.getline(name,MAX);
• cout<<"Enter Department"<<endl;
• cin.getline(department,MAX);
• cout<<"enter the basic salary"<<endl;
• cin>>salary;
• cout<<"enter the age"<<endl;
• cin>>age;

•void employee::display(void)
•{
•cout<<employeeID<<endl;
•cout<<name<<endl;
cout<<department<<endl;
•cout<<salary<<endl;
cout<<age<<endl;
•}
•void main()
•{
• employee Myemployees;
• Myemployees.getdata();
• Myemployees.display();
Assignment
1. The coordinates of a point on a line consists of x
and y values. Write a C++ program that would
implement a class named line with the following
properties:
 Data members as x and y;
 A member function for initializing the values of x and y to 0;
 A member function for initializing two points A(0,1) and
B(4,6) on the line;
 A member function for creating a third point by adding the
corresponding x and y coordinates of the two points on the
line.
• N/B The program should display the coordinates of
the created point.
2. Write a C++ program that would
instantiate a class named integer with
the following properties:
 A data member named a.
 A member function named input for
setting the value of a to 10.
 A member function named display for
outputting the value of a. [5 marks]
 N/B member functions, input and display
should be used outside the class integer

You might also like