0% found this document useful (0 votes)
2 views8 pages

2 Lab

This lab focuses on understanding classes and data abstraction in object-oriented programming. It includes tasks to create classes for displaying messages, counting objects, and imitating basic data types, along with post-lab tasks for handling time and marks. The conclusion emphasizes the student's ability to declare classes, objects, and member functions, as well as the importance of constructors and destructors.

Uploaded by

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

2 Lab

This lab focuses on understanding classes and data abstraction in object-oriented programming. It includes tasks to create classes for displaying messages, counting objects, and imitating basic data types, along with post-lab tasks for handling time and marks. The conclusion emphasizes the student's ability to declare classes, objects, and member functions, as well as the importance of constructors and destructors.

Uploaded by

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

LAB#02

Classes and Data Abstraction

LAB OBJECTIVE:
Objective of this lab is to understand the importance of classes and construction of objects using
classes.

INTRODUCTION:
3.1 Class & Object:

The fundamental idea behind object-oriented languages is to combine into a single


unit both data and the functions that operate on that data. Such a unit is called an object. A class
serves as a plan, or blueprint. It specifies what data and what functions will be included in
objects of that class. An object is often called an “instance” of a class Syntax:

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>

using namespace std;

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.

cout<<"I am object no "<<ob<<endl;

}
};
int main()
{

object o1(1),o2(2),o3(3); // object with single arguments to pass


these values to constructor.
//calling functions to take result of functions defined in calss.

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);

cout<<"Total onjects created are "<< i<<endl;


}

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>

using namespace std;

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 classes and objects.

The student will be able to declare member functions & member variables of class

He will understand the importance and use of constructor.

He will understand the use of destructor in class.

NOTE:font: Times New Roman, Heading font 14, statement font 12

You might also like