Mr. Fourcan Karim Mazumder Faculty Dept. of Computer Science and Engineering
Mr. Fourcan Karim Mazumder Faculty Dept. of Computer Science and Engineering
Program:
#include<iostream.h>
//accessing private data using member function
class myclass
{
int a;
public:
void set_a(int num);
int get_a();
};
void myclass::set_a(int num)
{
a=num;
}
int myclass::get_a()
{
return a;
}
int main()
{
myclass ob1, ob2;
ob1.set_a(10);
ob2.set_a(99);
cout<<ob1.get_a()<<"\n";
cout<<ob2.get_a()<<"\n";
return 0;
}
output: 10
99
#include<iostream.h>
//accessing public data using objects
class myclass
{
public:
int a;
};
int main()
{
myclass ob1, ob2;
ob1.a=10;
ob2.a=99;
cout<<ob1.a<<"\n";
cout<<ob2.a<<"\n";
return 0;
}
output: 10
99
#include<iostream.h>
//displaying value by using two objects.
class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void)
{
cout<<"number:"<<number<<"\n";
cout<<"cost:"<<cost<<"\n";
}
};
void item::getdata(int a, float b)
{
number=a;
cost=b;
}
int main()
{
item x;
cout<<"\n object x"<<"\n";
x.getdata(100,299.95);
x.putdata();
item y;
cout<<"\n object y"<<"\n";
y.getdata(200,175.50);
y.putdata();
return 0;
}
output:
object x
number:200
cost:299.95
object y
number:100
cost:175.50
Assignment 1:
Write a program that will demonstrate the access
of member functions by objects. The program that
uses the following properties- class name- A,
integer type private member variable- b, two public
member functions-set_data(int value), getdata(),
four objects- e, f, g, h. The output will be:
140
99
38
28
Assigning object:
One object can be assigned to another provided
that both objects are of the same type. By default,
when one object is assigned to another, a copy of
all the data members is made.
#include<iostream.h>
//Assigning objects for same class type objects
class myclass
{
int a, b;
public:
void set(int i, int j)
{
a=i;
b=j;
}
void show()
{
cout<<a<<' '<<b<<"\n";
}
};
int main()
{
myclass o1, o2;
o1.set(10, 4);
//Assigning o1 to o2
o2=o1;
o1.show();
o2.show();
return 0;
}
output:
10 4
10 4
#include<iostream.h>
// assigning objects for different class type objects.
class myclass
{
int a, b;
public:
void set(int i, int j)
{
a=i;
b=j;
}
void show()
{
cout<<a<<' '<<b<<"\n";
}
};
class yourclass
{
int a,b;
public:
void set(int i, int j)
{
a=i;
b=j;
}
void show()
{
cout<<a<<' '<<b<<"\n";
}
};
int main()
{
myclass o1;
yourclass o2;
o1.set(10,4);
o2=o1;
o1.show();
o2.show();
return 0;
}
output:
it will give a compile time error.
Assignment 2:
Write a program that will demonstrate assigning
objects. The program that uses the following
properties- class name- B, integer type private
member variable- c, d, two public member
functions- set_data(int m, int n), display(), two
objects- e, f.
The output will be:
67 55
67 55
Inline Function:
Inline functions can execute much faster than
normal functions. If inline function is too large,
then program grows larger (become slower). So, in
general, only short functions are declared as inline
functions. To declare an inline function, simply
precede the function’s definition with the inline
specifier.
Program:
#include<iostream.h>
//inline function description
class samp
{
int i, j;
public:
samp(int a, int b);
int divisible();
};
samp::samp(int a, int b)
{
i=a;
j=b;
}
inline int samp::divisible()
{
return !(i%j);
}
int main()
{
samp ob1(10,2), ob2(10,3);
if(ob1.divisible()) cout<<"10 is divisible by 2\n";
if(ob2.divisible()) cout<<"10 is divisible by 3\n";
return 0;
}
output:
10 is divisible by 2
Assignment 3:
Write a program that will demonstrate inline
function. The program that uses the following
properties- class name- aa, integer type private
member variable- a, b, one argumented
constructor function - aa(int i, int j), one int type
member function add(), one argumented object
ob(20, 10). add() function will add the value 20+10
and the output will be 30.
Automatic Inline function:
If a member function’s definition is short enough,
the definition can be included inside the class
declaration. This kind of function is called
automatic inline function. When a function is
defined within a class declaration, the inline
keyword is no longer necessary.
Program:
#include<iostream.h>
//Automatic inline function
class samp
{
int i,j;
public:
samp(int a, int b);
int divisible()
{
return !(i%j);
}
};
samp::samp(int a, int b)
{
i=a;
j=b;
}
int main()
{
samp ob1(10,2), ob2(10,3);
if(ob1.divisible()) cout<<"10 is divisible by 2\n";
if(ob2.divisible()) cout<<"10 is divisible by 3\n";
return 0;
}
output:
10 is divisible by 2
Assignment 4:
Write a program that will demonstrate automatic
inline function. The program that uses the
following properties (values are same as
assignment 3)- class name- aa, integer type
private member variable- a, b, one argumented
constructor function - aa(int i, int j), one int type
member function add(), one argumented object
ob(20, 10). add() function will add the value 20+10
and the output will be 30.
Nesting of member functions:
A member function can be called by using its name inside another member
function of the same class is known as nesting of member functions.
program:
#include<iostream.h>
//nested member function
class squarenumber
{
int number;
public:
long square();
void getnumber();
void display();
};
void squarenumber::getnumber()
{
cout<<"Enter an integer number:";
cin>>number;
}
long squarenumber::square()
{
number=number*number;
return (number);
}
void squarenumber::display()
{
cout<<"Square of the number:"<<square();
}
int main()
{
squarenumber sqr;
sqr.getnumber();
sqr.display();
return 0;
}
output:
enter an integer number:12
square of the number:144
#include<iostream.h>
//nested member function
class set
{
int m, n;
public:
void input();
void display();
int largest();
};
void set::input(void)
{
cout<<"Input values of m and n"<<"\n";
cin>>m>>n;
}
int set::largest(void)
{
if(m>=n)
return(m);
else
return(n);
}
void set::display(void)
{
cout<<"Largest value="<<largest()<<"\n";
}
int main()
{
set a;
a.input();
a.display();
return 0;
}
output:
Input values of m and n
20
10
Largest value is 20