Oop Lab Manual: Name: Bushra Aqeel ROLL NO: 003 Section: 2M Submitted To: Sir Haider Ali
Oop Lab Manual: Name: Bushra Aqeel ROLL NO: 003 Section: 2M Submitted To: Sir Haider Ali
CODE:
#include<iostream>
using namespace std;
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;
}
void printDate()
{
cout<<"Today Date is......"<<day<<"/"<<month<<"/"<<year;
}
};
int main()
{
Date d;
d.setDate();
d.printDate();
}
OUTPUT
TASK N0#02
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.
CODE:
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
string city;
public:
void inputDetails(string n, int a, string c) {
name = n;
age = a;
city = c;
}
int getAge() {
return age;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "City: " << city << endl;
}
};
int main() {
Person person1, person2;
string name1 = "Ali";
int age1 = 23;
string city1 = "Lahore";
person1.inputDetails(name1, age1, city1);
string name2 = "Hassan";
int age2 = 20;
string city2 = "Jhang";
person2.inputDetails(name2, age2, city2);
if (person1.getAge() > person2.getAge()) {
person1.display();
} else {
person2.display();
}
return 0;
}
OUTPUT
TASK N0#03
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
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
CODE:
#include<iostream>
using namespace std;
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;
}
int 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");
}
OUTPUT
TASK NO#04
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
. You have to create two objects and display the values inside them.
CODE:
using namespace std;
class Student {
private:
float age;
public:
Student(int input_age) {
if (input_age > 7 && input_age < 30) {
age = input_age;
} else {
cout<<"You have entered an invalid value." <<endl;
}
}
void show(){
cout << "Age of student: " <<age<<endl;
}
};
int main(){
Student student1(20);
Student student2(15);
student1.show();
student2.show();
return 0;
}
OUTPUT
TASK NO#05
Write a C++ program using parameterize constructor to find the area of
rectangle.
CODE:
#include <iostream>
using namespace std;
class Rectangle {
private:
float length;
float width;
public:
Rectangle(float l, float w) {
length = l;
width = w;
}
float calculateArea() {
return length * width;
}
};
int main() {
Rectangle rect(5.0, 3.0);
cout << "Area of rectangle: " << rect.calculateArea() <<
endl;
return 0;
}
OUTPUT
TASK NO#06
Write a C++ program using Copy Constructor to find the area of rectangle.
#include <iostream>
using namespace std;
class Rectangle {
private:
float length;
float width;
public:
Rectangle(float l, float w) {
length = l;
width = w;
}
Rectangle(const Rectangle &obj) {
length = obj.length;
width = obj.width;
}
float calculateArea() {
return length * width;
}
};
int main() {
Rectangle rect1(5.0, 3.0);
Rectangle rect2 = rect1;
cout << "Area of rectangle 1: " << rect1.calculateArea() <<
endl;
cout << "Area of rectangle 2 (the one we copied): " <<
rect2.calculateArea() << endl;
return 0;
}
OUTPUT
TASK NO#07
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
#include <iostream>
using namespace std;
class AverageCalculator {
private:
int num1;
int num2;
public:
AverageCalculator(){
num1=100;
num2=100;
}
void avg() {
double average = (num1 + num2) / 2.0;
cout << "The avg is: " << average <<endl;
}
};
int main() {
AverageCalculator obj;
obj.avg();
return 0;
}
OUTPUT
TASK NO#08
Define a class “Bank: that includes the following data members:
Name of Depositor
Account Number
Balance Amount
Name->Ahmed
Balance Amount->100
Account Number->123
CODE:
#include<iostream>
#include<string>
using namespace std;
class Bank {
private:
string depositorName;
int accountNumber;
double balanceAmount;
public:
Bank(string name = "Ahmed", int accNum = 123, double balance =
100){
depositorName=name;
accountNumber=accNum;
balanceAmount=balance;
}
void deposit(double amount) {
balanceAmount =balanceAmount+amount;
}
void withdraw(double amount) {
if (balanceAmount >= amount) {
balanceAmount =balanceAmount- amount;
} else {
cout << "Insufficient balance." <<endl;
}
}
void display() {
cout << "Name: " << depositorName << ", Account Number: "
<< accountNumber
<< ", Balance: " << balanceAmount <<endl;
}
};
int main() {
Bank account("Hassan", 123, 100);
double depositAmount = 20;
account.deposit(depositAmount);
cout << "Balance amount after deposit:" <<endl;
account.display();
double withdrawAmount = 30;
account.withdraw(withdrawAmount);
cout << "Balance amount after withdraw:" <<endl;
account.display();
return 0;
}
OUTPUT
TASK NO#09
C++ Program to initialize pointers.
CODE:
#include <iostream>
using namespace std;
int main(){
int var = 5;
int* pointVar;
// store address of var
pointVar = &var;
// print var
cout << "var = " << var << endl;
// print *pointVar
cout << "*pointVar = " << *pointVar << endl
<< endl;
cout << "Changing value of var to 7:" << endl;
// change value of var to 7
var = 7;
// print var
cout << "var = " << var << endl;
// print *pointVar
cout << "*pointVar = " << *pointVar << endl;
cout << "Changing value of *pointVar to 16:" << endl;
// change value of var to 16
*pointVar = 16;
// print var
cout << "var = " << var << endl;
// print *pointVar
cout << "*pointVar = " << *pointVar << endl;
return 0;
}
OUTPUT
TASK NO#10
C++ program of pointers through array
CODE:
#include <iostream>
using namespace std;
class My_Class {
int num;
public:
void set_number(int val) {num = val;}
void show_number();
};
void My_Class::show_number()
{
cout << num << "\n";
}
int main()
{
My_Class object[2], *p;
object[0].set_number(10); // objects is accessed directly
object[1].set_number(20);
p = &object[0]; // the pointer is obtained to the first element
p->show_number(); // value of object[0] is shown using pointer
p++; // advance to the next object
p->show_number(); // show value of object[1] is shown using the
pointer
p--; // retreat to previous object
p->show_number(); // again value of object[0] is shown
}
OUTPUT
TASK NO#11
C++ program to find the area of rectangle using pointers
CODE:
#include <iostream>
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle(int l, int b)
{
length=l;
breadth=b;
}
int getArea()
{
return 2*length*breadth;
}
};
int main()
{
// creating an object of Rectangle
Rectangle var1(5,2); // parameterized constrcutor
/* creating a pointer of Rectangle type &
assigning address of var1 to this pointer */
Rectangle* ptr = &var1;
/* calculating area of rectangle by using pointer
ptr to call the objects getArea() function
*/
int area = ptr->getArea();
cout<<"Area of rectangle is: "<<area;
return 0;
}
OUTPUT
TASK NO#12
Create a class named IceCreamCone with fields for flavor,number of scoops, type
of cone, and price. Unless arguments are supplied, flavor defaults to “Vanilla”,
number of scoops defaults to 1, and cone type defaults to “Sugar”.Theconstructor
calculates the\ price based on 75 cents per scoop with an additional 40 cents for a
waffle cone.
CODE:
#include<iostream>
using namespace std;
class icecreamcone {
private:
string flavor;
int numscoops;
string conetype;
int price;
public:
icecreamcone(string flavor = "Vanilla", int numscoops = 1,
string conetype = "Sugar") {
this->flavor = flavor;
this->numscoops = numscoops;
this->conetype = conetype;
TASK NO#13
Create a Person class that includes fields for last name, first name, and zip code.
Include a default constructor that initializes last name, first name, and zip code to
“X” if no arguments are supplied. Also include a display function.
CODE:
#include<iostream>
using namespace std;
class Person {
private:
string lastname;
string firstname;
string zipcode;
public:
Person(){
lastname="X";
firstname="X";
zipcode="0";
}
Person(string lastname, string firstname, string zipcode){
this->lastname=lastname;
this->firstname=firstname;
this->zipcode=zipcode;
}
void display() {
cout << "Last Name: " << lastname<<endl;
cout << "First Name: " << firstname<<endl;
cout << "ZIP Code: " << zipcode <<endl;
}
};
int main(){
Person person1;
Person person2("Amin", "Ahmad", "922");
cout << "Default Values:" <<endl;
person1.display();
cout << endl;
cout << "Provided Values:" <<endl;
person2.display();
return 0;
}
OUTPUT