Professional Documents
Culture Documents
Lab 1 Tasks
Topics covered:
Create classes and objects
Implement classes using member functions
Constructor Types
Problem Statement 1:
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();
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.
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.