LAB 2 Object Oriented Programming
LAB 2 Object Oriented Programming
1. OBJECTIVES :
Introduction to syntax of class, its data members, member variables and
member functions.
To Differentiate between public and private .
To know about Constructors and destructors of class and their syntax.
Repeated concept about counters a counter is variable that counts things.
2. INTRODUCTION.:
Declaration and definition of class. The definition starts with the keyword
“class” followed by the class “name”. Data is concealed within the class to
avoid data manipulation. Private data cannot be accessed outside the class and
public data can be accessed from outside the class. Constructor is executed
automatically whenever an object is created and name of constructor is same
as the class name. Destructors is same as the constructors but have tilde(~)
sign before name and it is called when object is destroyed.
3. IN LAB TASKS
i. TASK # 1
Write a class that displays a simple message “I am object no. __”, on the screen whenever
an object of that class is created.
TASK CODE :
1 //Hamna Baig
2 //FA20-BEE-030
3
4 #include <iostream> //Standard library of C++
5 using namespace std;
6 class counter //decalaration of class
7 {
8 private: //Not accessible from outside the class
9 static int counts; //static variable declaration as counter
10 public: //Accessible from outside the class
11 counter() //No argument Constructor
12 {
13 counts++; //incrementation of counter
14
15
16 cout << "I am an object "<<counts<<endl; //To show output
17 }
18 };
19 int counter::counts=0; //Initialization of static variable used as counter
20
21 int main() //Main function
22 {
23 counter Class_1,Class_2,Class_3; //Declaration of objects of class
24 }
25
OUTPUT :
ii. TASK # 2 :
Write a program to calculate the number of objects created and destroyed for
the counter class.
TASK CODE :
1 //Hamna Baig
2 //FA20-BEE-030
3
4 #include <iostream> //Standard library of C++
5 #include <iostream> //Standard library for c++
6 using namespace std;
7
8 class Counter //Declaration of class
9 {
10 private: //not accessible from outside the class
11 static int created, destroyed; //declaration of static variables
12 //used as counter for objects created and destroyed
13 public: //accessible from outside the class
14 Counter() //constructor declaration
15 {
16 created++; //To increment objects created count when object created
17 }
18 ~Counter() //Declaration of destructor
19 {
20 destroyed++; //To increment objects destroyed count when object destroyed
21 if(created==destroyed) //objects created must be equal to objects destroyed
22 {
23 cout<<"Objects created are :"<<created<<" and destroyed are "<<destroyed<<endl;
24 //To show output
25
}
26
}
27
};
28
int Counter::created=0; //Initialization of created
29
int Counter::destroyed=0; //Initialization of destroyed
30
int main() //main function
31
{
32
Counter c1,c2,c3,c4,c5,c6; //declaration of objects of class
33
return 0;
34
}
35
OUTPUT :
iii. TASK # 3 :
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.
TASK CODE :
OUTPUT
TASK CODE :
1 //Hamna Baig
2 //FA20-BEE-030
3
4 #include<iostream> //Standard library function
5 using namespace std;
6
7 class Time //Declaration of class Time
8 {
9 private: //Not accessible from outside class Time
10 int hours,minutes,seconds; //Declaration of variables
11 public: //Accessible from outside class Time
12 Time(int h,int m,int s):hours(h),minutes(m),seconds(s) //3 argument constructor
13 {
14 //Empty body
15 }
16
17 void display_standardforms() //Function to display time in standard forms
18 {
19 cout<<""<<hours<<" : "<<minutes<<" : "<<seconds<<endl; //To print time provided by user
20
21
22 if(hours<12) //If hours less than 12 means in 12 hour format it is time in AM
23 {
24 cout<<"Time in 24 hour format : ";
25 cout<<"\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" AM "; //To show in 24 hour format
26 cout<<"\nTime in 12 hour format : ";
27 cout<<"\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" AM ";
28 }
29 else //execute when hours greater than 12
30 { //If hours > 12 means in 12 hour format it is time in PM
31 cout<<"Time in 24 hour format :";
32 cout<<"\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" PM ";
33 hours=hours-12; //we minus 12 from hours to show in 12 hour format
34 cout<<"\nTime in 12 hour format :";
35 cout<<"\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" PM ";
36 }
37 if(hours==12)
38 { //When time is 12 it is 12 in 12 hour format but 00 in 24 hour format
39 cout<<"Time in 12 hour format :";
40 cout<<"\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" PM ";
41 hours=hours-12; //To show i2 in 24 hour format we minus 12 from it
42 cout<<"\nTime in 24 hour format :";
43 cout<<"\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" PM ";
44 }
45
46 }
47 };
48
int main()
49 {
50 Time T1(11,34,56); //Declaration of objects of class Time and passing argument
51 Time T2(23,34,56);
52 cout<<"Time 1 given by user is : "<<endl;
53 T1.display_standardforms(); //Calling of member function to display time for object 1
54 cout<<"\n\nTime 2 given by user is : "<<endl;
T2.display_standardforms(); //Calling of member function to display time for object 2
cout<<endl<<endl;
OUTPUT :
ii. TASK # 2 :
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.
TASK CODE :
1 //Hamna Baig
2 //FA20-BEE-030
3
4 #include<iostream> //standard library for C++
5 using namespace std;
6
7 class marks //Declaration of class marks
8 {
9 private: //Not accessible from outside the class
10 int subject_1,subject_2,subject_3;
11 float sum_of_marks;
12 float average_of_marks;
OUTPUT :
5. LAB CONCLUSION :
Understanding the logics of programs and implementing in C++ coding to design a
program using classes .
Understand the concept of defining and declaring classes and its objects.
To declare member data in private and declaring member functions in public so they
are accessible from main function. Main function cannot call member data in private
so whatever we want to do with member data do it in member functions.To get result
we call member functions in main function.
The use of constructor ,they are called automatically whenever an object is created
and are used for automatic initialization..
Destructors are called when an object is destroyed it has the same name as of the class
but preceded by the sign(~).
Arguments can also be passed to the constructors.
------------------------------------------------------------------------------------