You are on page 1of 5

Superior University

Department of Computer Science Robotics.


OBJECT ORIENTED PROGRAMMING LAB Tasks

Lab 1 Tasks
Topics covered:
 Create classes and objects
 Implement classes using member functions
 Constructor Types

Problem Statement 1:

Write a program to input date(day,month,year) by a member function and


print the date(day,month,year) by writing the another member function.

Solution:
#include<iostream.h>
#include<conio.h>

class Date
{
private:
int year,month,day;

public:
void setDate( )
{
cout<<"Enter Year ";
cin>>year;
cout<<"Enter Month";
cin>>month;
cout<<"Enter Date";
cin>>day;

}//end of setDate

void printDate()
{
Superior University
cout<<"Today Date is......"<<day<<"/"<<month<<"/"<<year;
}//end of printDate

};//end of Date

Void main()
{
Date d;
d.setDate();
d.printDate();

Sample Input Sample Output


Year : 2001, Month: 12, Day: 23 23/12/2001

Problem Statement 2:
Write a program by using a class that takes name, age and city of a person as
class attributes. An input Details member functions to input the data, getAge
function to return age and Display function to display name , age and city.Input
the data for two persons and display the record of the person who is elder in age.

Input for person 1 Input for person 2 Output


Ali , 23, Lahore. Hassan , 20 , Jhang Ali , 23, Lahore
////////elder in age

Problem Statement 3:
Superior University
Define a Class Distance with private attributes Feet (int) and Inches (float). Also,
define public member functions: - Set distance() – to assign values to the class
attributes - Display distance() – to display the values of class attributes Include a
main() program that creates an instance (object) of type Distance. Ask the user
to enter the value of Distance variables and call the appropriate functions to set
the object value and then display the object value.
Define the class member functions outside the class using scope resolution operator
(::).

Solution:
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Distance{
int Feet;
float Inches;
public:
void Set_distance(int, float);
void Display_distance();
};
void Distance::Set_distance(int aFeet, float aInches){
Feet = aFeet;
Inches = aInches;
}
void Distance::Display_distance(){
cout << "The distance is: " << Feet << " feet and " << Inches << " inches." << endl;
}
void main()
{
Distance d1;
int dFeet;
float dInches;
cout << "Enter distance in Feet: ";
cin >> dFeet;
cout << "Enter distance in Inches: ";
cin >> dInches;
d1.Set_distance(dFeet, dInches);
d1.Display_distance();
system("pause");
}

Problem Statement 4:
Superior University
Write a class of Student that has age as data members. A constructor with one
parameter initializes data members input value if input value is>7 and<30 with
given value
Else displays message:
“You have entered Invalid value”
and member function show displays the age of student
. You have to create two objects and display the values inside them.

Problem Statement 5:
Write a C++ program using parameterize constructor to find the area of
rectangle.

Problem Statement 6:
Write a C++ program using Copy Constructor to find the area of rectangle.

Problem Statement 7:
Write a class that contains two integer data members which are initialized to
100 when an object is created. It has a member function avg that displays the
average of data members.

Sample output: The avg is: 100

Problem Statement 8 Bonus program:


Define a class “Bank: that includes the following data members:
 Name of Depositor
 Account Number
 Balance Amount

The class also contains the following member functions:


 A parameterized constructor to initialize :
 Name->Ahmed
 Balance Amount->100
 Account Number->123
Superior University
 Deposit Function to deposit some amount. It should accept amount as a
parameter.
 Withdraw function to withdraw an amount. It should also accept
amount as a parameter.
 Display function to display name and balance amount after deposit and
withdraw.
Sample Initialized Sample Output Sample Output
Input Balance amount after Balance amount after
deposit withdraw
Hassan, 100,123 100+20=120 120-30=90

You might also like