Home | | Computer Science 11th std | C++: Functions in a class

Example Programs in C++ - C++: Functions in a class | 11th Computer Science : Chapter 14 : Classes and objects

Chapter: 11th Computer Science : Chapter 14 : Classes and objects

C++: Functions in a class

Objects accessing inline and outline member functionsA wide range of operations for the class members are performed using member functions of the class .

Functions in a class


A wide range of operations for the class members are performed using member functions of the class. These operations are defined in the member function.

 

Objects accessing inline and outline member functions

 

As you are aware the definition of the member function can be given either inside the class specification or outside the class

Let us see from the illustrated example program. This program defines a class called sales with the following description

 

private members of class salesman

sno : integer(salesperson number)

sname : 15 characters       

hrwrk ,wage : float (hours worked and wage per hour)

totwage: float(hrwrk * wage)   

calcwg( ) : a function to find hrwrk * wage and store it in totwage

 

public members of class salesman

in_data( ) : a function to accept values for sno, sname, hrwrk, wage and invoke calcwg( ) to calculate totwage

out_data( ) : a function to display all the data members on the screen. you should give definitions of functions as outline



 

Illustration 14.6 Inline Outline and function

 #include<iostream>

 using namespace std

 class sales

 {

      int sno;

       char sname [15];

       float hrwrk,wage,totwage;

 void calcwg( )

 {

        totwage=hrwrk * wage;

 }

public :

       void in_data();

       void out_data();

 };

void sales :: in_data()

{

      wage=75.26;

      totwage=0.0;

      cout<<"\nEnter the salesperson id";cin>>sno;

      cout<<"\nEnter the name" ;gets(sname);

      cout<<"\nEnter the hours worked" ;cin>>hrwrk;

      calcwg(); //member function called inside by another member function

}

void sales :: out_data()

{

      cout<<"\n Wage slip ";

      cout<<"\n ~~~~~~~~ ";

      cout<<"\nID : " << sno;

      cout<<"\nName : " <<sname ;

      cout<<"\nHours worked :" <<hrwrk;

      cout<<"\nTotal Wage :"<<setprecision(2)<<totwage;

}

int main()

{

      sales sal;

      sal.in_data();

      sal.out_data();

      return 0;

}

Output:

Enter the salesperson id 1201

Enter the name ARUL

Enter the hours worked 7

Wage slip

~~~~~~~~

ID : 1201

Name : ARUL

Hours worked :7

Total Wage :526.82

 

Tags : Example Programs in C++ , 11th Computer Science : Chapter 14 : Classes and objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 14 : Classes and objects : C++: Functions in a class | Example Programs in C++

Related Topics

11th Computer Science : Chapter 14 : Classes and objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.