2 Lab
2 Lab
LAB OBJECTIVE:
Objective of this lab is to understand the importance of classes and construction of objects using
classes.
INTRODUCTION:
3.1 Class & Object:
IN-LAB TASKS
Task#01
Write a class that displays a simple message “I am object no. __”, on the screen whenever an
object of that class is created.
Code:
#include<iostream>
class object
{
private:
int ob;
public:
object(int i) //contructor is used with single parameter
to assign value to object in class.
{
ob=i;
}
void obj(){ // function definition in a class to
access the value of ob.
}
};
int main()
{
o1.obj();
o2.obj();
o3.obj();
}
console window
Task#
Write a program to calculate the number of objects created and destroyed for the counter class.
Code:
// a program to calculate the number of objects created
#include<iostream>
using namespace std;
class count
{
private:
int id;
public:
count (int i)
{
id=i;
}
};
int main()
{
int i=0;
count c1(++i), c3(++i);
console window
Task#03
Create a class that imitates part of the functionality of the basic data type ‘int’, call the class Int.
The only data in this class is an integer variable. Include member functions to initialize an Int to
0, initialize it to an ‘int’ value, to display it, and to add two Int values. Write a program that
exercises this class by creating one uninitialized and two initialized Int values, adding the two
initialized Int values and placing the response in uninitialized value and then displaying the
result.
Code:
#include<iostream>
using namespace std;
class Int
{
private:
int I;
public:
Int (int a)
{
I=a;
}
void show(){
cout<<"I = "<< I<<endl;
}
void add(Int a,Int b){
I=a.I+b.I;
}
void shw(){
cout<<"Sum = "<< I <<endl;
}
Int ()
{
I=0;
}
};
int main()
{
Int i1(3),i2(5),i3;
i1.show();
i2.show();
i3.add(i1,i2);
i3.shw();
}
console window
POST-LAB TASKS
Task# Create a class named time, the data members are hours, minutes and seconds. Write a
function to read the data members supplied by the user, write a function to display the data
members in standard (24) hour and also in (12) hour format.
Code:
#include <iostream>
class Time{
private:
int hours, minutes, seconds;
public:
void readData(){
cout<<"\nEnter hours: ";
cin>>hours;
cout<<"\nEnter minutes: ";
cin>>minutes;
cout<<"\nEnter seconds: ";
cin>>seconds;
}
void display(){
if (hours>12){
cout<<"\nTime in 24 hours format is :";
cout<<"\n"<<hours<<":"<<minutes<<":"<<seconds;
cout<<"\nTime in 12 hour format:\n";
hours=hours-12;
cout<<"\n"<<hours<<":"<<minutes<<":"<<seconds;
}
If(hours<12){
cout<<"\nTime in 12 hour format:\n";
cout<<"\n"<<hours<<":"<<minutes<<":"<<seconds;
}
}
};
int main()
{
Time t;
t.readData();
t.display();
return 0;
}
console window
Task# Write a class marks with three data members to store three marks. Write three member
functions, set_marks() to input marks, sum() to calculate and return the sum and avg() to
calculate and return average marks. Write a program that exercises this class by creating its
objects and displaying results.
Code:
#include<iostream>
using namespace std;
class marks{
private:
int mark1;
int mark2;
int mark3;
public:
void set_marks(){
cout<<"Enter mark 1: ";
cin>>mark1;
cout<<"Enter mark 2: ";
cin>>mark2;
cout<<"Enter mark 3: ";
cin>>mark3;
}
int sum(){
return mark1+mark2+mark3;
}
int avg(){
return (float)( sum())/3.0;
}
};
int main() {
marks mark;
mark.set_marks();
cout<<"Sum: "<<mark.sum()<<"\n";
cout<<"Avg: "<<mark.avg()<<"\n";
return 0;
}
console window
Conclusion:
The student will be able to declare member functions & member variables of class