Friend function:-
- The concept of friend function goes against the concept of date hiding.
- The friend function is declared within the class and it is defined outside of the class.
- The friend function is not a member function of the class.
- Since friend function is not the member function of the class. It is not called for any object, rather objects are passed as parameters to it.
- Even if friend function is not a member function of the class. It can access private member of the class.
- The concept of friend function is useful when a function act as friend function of two more classes.
Consider the following example.
Program :-
Develop a class period represented in hours and minutes. The class should have following member function.
1)Setperiod ( ) 2) getperiod ( ) 3) display ( ) Define a fn add as a friend fn of class period .It will add two periods and should return answers of the addition.
Write suitable main fn
# include
- Encapsulation:
Defining data members and member functions together within a class is refffered as encapsulation. Without encapsulation the object oriented programming cannot be implement.
Data hiding:
In object oriented programming most of the item data members is defined as private and member function will be defined as public. The public members of the class can be accessed from inside of the class as well as from the outside of the class. The private members of the class can be accessed from inside of the class but cannot be accessed from outside of the class. Thus private members remain hidden from outside of the class. Since most of the time the data members are kept private it remains hidden from outside of the class and thus the data hiding is implemented. Since data members are private the class user cannot access them directly but ca access them through predefined functions. Thus the class user can access the data the way class developer likes it.
- A function set radius that accepts value of the radius as parameter.
- A function get radius that will get the value of radius from user.
- A function to calculate and return area of the function.
- A function to calculate and return circumference of the circle.
- Write a suitable main function
Explanation:-
- Encapsulation:
In the above program data member radius and four member function set radius, get radius, area and circum are defined together and thus the encapsulation is obtained
II.Data hiding:
The private members of the class can be accessed from inside of the class can be accessed from inside of the class but cannot be accessed from outside of the class. In the previous example the private member radius is accessed within the class and it cannot be access from the main function.
→ Public members:
Objects and allocated memory.
Calling the member fn.
Introduction to structures :-
1) The c + + allows us to create our own data type. Due to this facility, programmer can create his or her own data type for a specific application.
2) A structure is nothing but group of data elements, which can be of same type as well as of different types.
Defining a structure data type :-
The general syntax of declaring structure data type is a follows.
struct datatype
{
datatype member 1 ;
datatype member 2 ;
……………………
……………………
……………………
datatype member n ;
} ;
As the general syntax indicates, the structure datatype is made up[ of members. The member can be of different data types.
Example :-
struct student
{
int roll-no ;
float percent ;
char name [ 40 ] ;
}
In the above example, the student is defined as structure datatype with members, roll-no of type integer, percent of type float and name which is array of 40 characters.
The structure data type is also called as structure tag.
Declaring variables of structure data type –
The general syntax of declaring a structure variable is as follows.
Structure datatype variable name.
example
struct student s1 ;
Due to the above example, the variable S1 of type struct student is declared. The compiler will allocate a space of 46 bytes to store the information of S1.
Method 1 : Method 2 :
struct student struct student
{ {
int roll_no; int roll_no ;
float percent; float percent ;
char name [ 40 ] ; char name [ 40 ] ;
} S1; }
struct student S1;
The net effect of both the above method is same.
Accessing individual Members of the structures
1) When we define a structure data type and its variable, then it becomes necessary to deal with every individual member of the structure.
2) The general syntax of accessing the structure members is as follows.
Structure variable name, member name.
Example :-
struct student
{
int roll_no ;
float percent ;
char name [ 40 ] ;
}
struct student S1 ;
…………………
…………………
S1. roll_ no = 12 ;
S1. percent = 86.33 ;
S1. name = “xyz” ;
………………..
………………..
As the example indicates, using three assignment statements, we have assigned information to every individual member of a structure.
Assignment of complete structure variable :-
Consider the following example
Example –
struct student
{
int roll-no ;
float percent ;
char name [ 40 ] ;
}
struct student S1, S2 ;
……………………..
……………………...
S1. roll_no = 2 ;
S1. percent = 87.67 ;
S1. name = “abc” ;
S2 = S1 ;
…………………….
…………………….
Due to the statement S2 = S1 ; the values of all the members of Slar copied into corresponding members of S2.
The effect of,
S2 = S1;
Can be shown as follows
The statement S2 = S1 ; carry out he job of three assignment statement as shown below.
S2. roll_no = S1. roll_no ;
S2. percent = S1. percent ;
S2. name = S1. name ;
Nested Structures / Embedded Structures / Structure within Structure / Structure as a members of structure:-
It is possible to define a structure as a member of the other structure resulting into nested structure. Consider the following example.
struct data
{
int dd,mm,yy ;
} ;
struct student
{
int roll_no ;
float percent ;
char name [ 40 ] ;
struct date birth date ;
} ;
struct student S1 ;
In this example we have defined structure data type date with three members dd, mm and yy. The student is also defined as structure data type with the following member.
roll_no of type int
percent of type float
name as array of 40 characters
birth date of type date.
It means that, member birthdate of structure –e student itself is another structure.
ARRAY OF STRUCTURES :-
1) We have defined a structure data type called as student. When we want to deal with information of number of students, then we can define array of struct student.
2) Consider the following example
struct student
{
int roll_no ;
char name [ 40 ] ;
float percent ;
} ;
struct student S [ 60 ] ;
In the above example, ‘S’ is defined as array of 60 elements and every element is of type struct student. In other word ‘S’ is an array of 60 students.
The member of the ith student can be accessed as follows.
S [i]. roll_no ;
S [i]. name
S [i]. percent
1) WAP that reads one date and prints the day number of the year.
→ # include
# include
struct date
{
int dd, mm, yy :
} ;
void main ( )
{
int year [ 13 ] = { 0, 31, 28, 31, 30, 31, 31, 30, 31, 30, 31}
struct date d1;
int i, sum = 0 ;
clrscr ( ) ;
cout << “Enter a date in dd mm yy format =” ;
cin >> dl.dd >> dl.mm>> dl.yy ;
for ( i – 1 ; i < d1.mm ; i + + )
sum = sum + year [i] ;
sum = sum + dl.dd ;
if (dl.mm >2)
if ( dl.yy % 400 = = 0 || dl.yy % 4 = = 0 & &. dl.yy % 100 ! = 0)
sum + + ;
cout << “It is day number” << sum “of the year” ;
getch ( ) ;
}
2) WAP that reads one date and prints ‘Happy New Year’ if it is first day of the year, otherwise it should print ‘Have a Nice Day’.
→ # include
# include
struct date
{
int dd. mm. yy ;
};
void main ( )
{
struct date dl ;
clrscr ( ) ;
cout << “Enter a date in dd. mm. yy format =” ;
cin >> dl. dd >> dl. mm >> dl. yy ;
if ( dl. dd = = 1 & & dl. mm = = 1 )
cout << “Happy New Year \n” ;
else
cout << “Have a Nice Day \n” ;
getch ( );
}
3) WAP that reads one today’s date and any other date. The pgm should print whether the other date has been already passed or yet to come or whether it is today’s date only.
→ # include
# include
struct date
{
int dd,mm. yy ;
} ;
void main ( )
{
struct td, ad ;
clrscr ( ) ;
cout << “Enter today’s date =” ;
cin >> td. dd >> td. mm >> td. yy ;
cout << “Enter other date =”;
cin >> ad. dd >> ad. mm >> ad. yy ;
if ( ad. yy > td. yy )
cout << “other date is yet to come \n” ;
else
if ( ad. yy < td. yy )
cout << “Other date has been passed \n” ;
else
if ( ad. mm > td. mm )
cout << “other date is yet to come \n” ;
else
if ( ad. dd > td. dd )
cout << “ It is today’s date only \n” ;
getch ( ) ;
}
4) WAP that reads roll_no and marks of PCM of not more than 60 students. Your pgm should calculate average of PCM of all
students and it should print roll_no and average of all students.
→ # include
# include
# include
struct student
{
int roll_no ;
int p, c, m,
float avg ;
} ;
void main ( )
{
struct student s[ 60 ] ;
int i , n ;
clrscr ( ) ;
cout << “How many students =” ;
cin >> n ;
cout << “Enter into all students \n” ;
for ( i = 0 ; i < n ; i + + )
{
cout << “Roll_no ;
cout << “marks of p , c, & m = ” ;
cin >> s[ i ] – p >> s[ i ] – c >> s[ i ] . m ;
s[ i ] . avg = ( s[ i ] p + s[ i ] c + s[ i ] . m
}
cout << “Roll_no \t average \n”;
for ( i = 0 , i < n ; i + + )
{
cout << setw ( 7 ) << s[ i ] . roll_no << ‘\t’ ;
cout << setw ( 7 ) << setprecision [ 2 ] << s[ i ]. avg << ‘\n’ ;
}
getch ( ) ;
}
1) WAP that reads information of not more than 100 employees. It consists of :
a)Employees_id
b)Employees_name
c)Designation
d) Salary
Your pgm should calculate allowances and taxes based on salary and should print the net amount payable to every employee. The details are as follows :
If salary > = 1000 then DA = 110% of salary
HRA = 15% of salary
TAX = 10% of salary
If salary > = 5000 and salary < then DA = 110% of salary
HRA = 20% of salary
TAX = 8% of salary
Otherwise DA = 100% of salary
HRA = 25% of salary
TAX = 2% of salary
→ # include
# include
# include
struct employees
{
int id ;
char name [ 40 ] ;
char designation [ 20 ] ;
float salary , da, hra, tax, net ;
} ;
void main ( )
{
struct employee e[ 100 ] ;
int i , n ;
clrscr ( ) ;
cout << “Enter info all employees =” ;
cin >> n ;
cout << “Enter info of all employees \n” ;
for ( i = 0 ; i < n ; i + + )
{
cout << “ id = ” ;
cin >> e[ i ] .id ;
cout << “ name = ” ;
gets ( e[i].name ) ;
cout << “ Designation = ” ;
gets ( e[i] .designation ) ;
cout << “ salary = ” ;
cin >> e[i].salary ;
for ( i = 0 , i < n ; i + + )
{
if ( e[i].salary > = 10000 )
{
e [ i ] . da = 1.1 * e [ i ] . salary ;
e [ i ] . hra = 0.15 * e [ i ] . salary ;
e [ i ] . tax = 0.1 * e [ i ] . salary ;
}
else
if ( e[ i ] . salary > = 5000 )
{
e [ i ] . da = 1.1 * e [ i ] . salary ;
e [ i ] . hra = 0.2 * e [ i ] . salary ;
e [ i ] . tax = 0.08 * e [ i ] . salary ;
}
else
{
e [ i ] . da = 1.1 * e [ i ] . salary ;
e [ i ] . hra = 0.25 * e [ i ] . salary ;
e [ i ] . tax = 0.02 * e [ i ] . salary ;
}
e [ i ] . net = e [ i ] . salary + e [ i ] . da + e [ i ] . hra – e [ i ].tax;
}
for ( i = 0 ; i < n ; i + + )
{
cout << “Id =” << e [ i ] . id << ‘\t’ << “Name =” << e [ i ] . name << ‘\n’ ;
cout << “Designation =” << e [ i ] . designation << ‘\n’ ;
cout << “Salary =” << e [ i ] . salary << ‘\n’ ;
cout << “DA =” << e [ i ] . da << ‘\t’ HRA =” << e [ i ] . hra << ‘\n’ ;
cout << “Tax =” << e [ i ] . tax << ‘\n’ ;
cout << “Net =” << e [ i ] . net << ‘\n’ ;
}
getch ( ) ;
}
Structure and pointer :
Using the pointer the structure variable can be accessed indirectly. Consider following example :
Structure student
{
int roll_no ;
Float percent ;
char name [ 40 ] ;
} ;
Struct student S1 ;
Struct student * P ;
- - - - - - - - - - -
- - - - - - - - - - -
p = & S1 ;
- - - - - - - - - - -
- - - - - - - - - - -
In the above example . . have define S1 as variable type struct student and p is defined type pointer to struct student.
- Consider the following statement :
P = & S1
Due to this statement, the p stores address of S1. In other word P will act as the pointer to S1.
- If we want to assign a value 10 to the roll_no of S1 then it can be done with three different methods
a) S1roll_no = 10 ;
b) (*p) . roll_no = 10 ;
c) P → roll_no = 10 ;
- The first method is without using pointer. Where as next two methods we use the pointers.
- the meaning of the second and third method is nothing but roll _no of that structure variable, which is being pointer by P. Since P point to S1, effectively second and third method result into S1 roll_no.
Program :
Write a function that accepts pointer to struct student and calculate the PCM for that student. write suitable main ( ) to read roll_no and marks of PCM for more than 60 students program should calculate the average of PCM fro every student using a function and should print roll_no. average of the PCM and class for every student.
Program :
→ # include
# include
struct students
{
int roll_no ;
int p, c, m ;
float avg ;
}
void main ( ){
struct student s[ 60 ] ;
int i , n ;
void cal_avg ( struct student * ) ;
clrscr ( ) ;
cout << “How many student ( n.m + 60 ) =” ;
cin >> n ;
cout << “Enter the information of all the students \n” ;
for ( i = 0 ; i < n ; i + + )
{
cout << “roll_no =” ;
cin >> s[i].roll_no ;
cout << “Marks of P, C & M =” ;
cin >> s[i]. p >> s[i] .m>>s[i].c ;
}
for ( i = 0 ; i > n ; i + + )
{
cout << “Roll_no =” << s [ i ] . roll_no << \t ;
cout << “Average =” << s [ i ] . avg << \t ;
if ( s [ i ] . avg > = 70 ) cout << “Distinction \n” ;
else if ( s [ i ] avg > 0 60 )
cout << “first class \n” ;
else
if ( s[i].avg >=50 )
cout <<” second class” \n ;
else
if ( s[ i ].avg > = 40)
cout << “Pass \n” ;
else
cout << “fail \n” ;
}
getch ( ) ;
}
void cal_avg ( struct student * q )
{
q → avg = q → p + q → + q → m ) /3.0 ;
}
void cal_avg ( struct student * q )
{
( * q ) . avg = ( c * q ) . P + ( * q ) . c + ( * q ) m \3.0 ;
}
Function with default parameters (arguments)
Or
Function with parameters having default values
Consider the following program.
# include
void main ( )
{
void disptime ( int = 8, int = 0, int = 0 );
disptime (,29,39);
disptime (8,30);
disptime (8);
disptime ( );
}
Void disptime (inthr, intmn, intsec )
{
cout << hr <<' : ' << sec << '\n';
}
O/P:- 8:29:39: 8:29:39
8:30:0 8:30:0
8:0:0 8:0:0
8:0:0 8:0:0
→ The prototype of function disptime indicates that it accepts three integers with default values 8, 0 and 0
→ In the first call of disptime we are passing all the three arguments. This will be copied into hr, min and sec and no default value will be utilized hence the o/p is 8:29:39
→ In the 2nd call only two actual parameters are passed. This parameters will be copied into hr and min. sec will get the default value i.e 0. Hence the o/p is 8:30:0
→ In the 3rd call only once actual parameters are passed. This parameters will be copied into hr. and sec will get the default value i.e and hence o/p is 8:0:0
→ In the 4th call no actual parameters are passéd. Hence hr, min and sec will take the default initial value i.e 8, 0 and 0 respectively hence o/p is 8:0:0
Program :-
# include
void main ( )
{
void dispdate ( int = 1, int = 1, int = 2006);
dispdate ( );
dispdate (26);
dispdate (15,8 );
dispdate ( 31,10, 2005 );
}
Void dispdate ( int d, int m, int y )
{
cout << d << '/' << m << '/' << y << '\n';
}
O/P:-
1/1/2006
26/1/2006
15/8/2006
31/12/2005
Objects and Classes
Introduction:-
In c language we can define our own datatypes using structures. Therefore it is possible to develop application oriented datatype. We can define data elements as members of structure in c. However c do not allow us to define functions as members of the structures.
To implement object oriented programming ,it is necessary to declare data members and associated functions together.
Therefore c do not support object oriented programming.
In c++ it is possible to define data as well as functions as members of the structure or members of the class.
Therefore c++ supports oject oriented programming.
Class:- is nothing but a datatype that defines set of objects having identical characteristics.
The class definition is often made up of data members and members functions.
Object:- The object is nothing but the physical existence of the class.
The object is also called as instance.
Consider the following eg:-
Class music-system
{
float length, breadth, depth;
float cost;
char color;
int no_of_speakers;
Public:
Void playcas ( );
void played cd ( );
void forward ( );
void rewind ( );
void record ( );
};
Music_system m1, m2;
→ In the above eg we have defined a class called music system which has got data members length, breadth, cost, color, depth, no of speakers.
→ The class have got following member function playcas, played, forward, rewind, record.
→ The m1 and m2 are defined as objects o6 class music system.
→ The members of the class will be accessed for the object as follows object name, member name.
More Articles …
Page 2 of 21