0% found this document useful (0 votes)
15 views11 pages

Practical Part A Answered

The document contains a series of C++ programming exercises focused on object-oriented programming concepts. It includes definitions and implementations of classes such as Student, Rectangle, Circle, and various others, demonstrating features like constructors, function overloading, inheritance, and encapsulation. Each exercise provides code snippets along with expected output for better understanding.

Uploaded by

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

Practical Part A Answered

The document contains a series of C++ programming exercises focused on object-oriented programming concepts. It includes definitions and implementations of classes such as Student, Rectangle, Circle, and various others, demonstrating features like constructors, function overloading, inheritance, and encapsulation. Each exercise provides code snippets along with expected output for better understanding.

Uploaded by

Asif Jahan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HS 2nd Year Practical Part A

Object Oriented Programming in C++


1. Define a class Student with data members name and roll. Input and display details of one
student.

#include<iostream>
using namespace std;
class Student {
string name;
int roll;
public:
void input() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll: ";
cin >> roll;
}
void display() {
cout << "Name: " << name << "\nRoll: " << roll << endl;
}
};
int main() {
Student s;
[Link]();
[Link]();
return 0;
}

Output:

Enter name: Rahul


Enter roll: 10
Name: Rahul
Roll: 10

2. Create a class Rectangle with data members length, breadth and display area.

#include<iostream>
using namespace std;
class Rectangle {
float length, breadth;
public:
void input() {
cout << "Enter length and breadth: ";
cin >> length >> breadth;
}
void area() {
cout << "Area = " << length * breadth << endl;
}
};
int main() {
Rectangle r;
[Link]();
[Link]();
return 0;
}

Output:

Enter length and breadth: 5 4


Area = 20

3. Define a class Circle with radius as data member. Include functions to set radius and calculate
the area using 3.14 * r * r.

#include<iostream>
using namespace std;
class Circle {
float radius;
public:
void setRadius(float r)
{
radius = r;
}
void area()
{
cout << "Area = " << 3.14 * radius * radius << endl;
}
};
int main() {
Circle c;
[Link](5);
[Link]();
return 0;
}
4. Write a program to demonstrate function overloading using a function add() that works for
two integers and two floats.

#include<iostream>
using namespace std;
class Addition {
public:
int add(int a, int b) {
return a + b;
}
float add(float x, float y) {
return x + y;
}
};
int main() {
Addition A;
cout << "Sum (int): " << [Link](10, 20) << endl;
cout << "Sum (float): " << [Link](2.5f, 3.7f) << endl;
return 0;
}

5. Define a class Person with constructor that takes name and age as arguments. Create objects
using parameterized constructor.

#include<iostream>
using namespace std;
class Person {
string name;
int age;
public:
Person(string n, int a)
{
name = n; age = a;
}
void display()
{
cout << name << " " << age << endl;
}
};
int main() {
Person p("Rahul", 19);
[Link]();
return 0;
}
6. Write a program to implement default constructor and display a default message when object
is created.

#include<iostream>
using namespace std;
class Demo {
public:
Demo()
{
cout << "Default constructor called!" << endl;
}
};
int main() {
Demo d;
return 0;
}

7. Write a program to create a copy constructor that copies data from one object to another.

#include<iostream>
using namespace std;
class Sample {
int x;
public:
Sample(int a)
{
x = a;
}
Sample(Sample &obj)
{
x = obj.x;
}
void show()
{
cout << "Value: " << x << endl;
}
};
int main() {
Sample s1(10);
Sample s2(s1);
[Link]();
return 0;
}
8. Create a class Employee with emp_id, name, and salary. Demonstrate constructor with
default arguments.

#include<iostream>
using namespace std;
class Employee {
int emp_id;
string name;
float salary;
public:
Employee(int id=101, string n="Unknown", float s=5000) {
emp_id = id; name = n; salary = s;
}
void display() {
cout << emp_id << " " << name << " " << salary << endl;
}
};
int main() {
Employee e1, e2(102, "Riya", 8000);
[Link]();
[Link]();
return 0;
}

9. Define a class Complex to add two complex numbers using object as function argument.

#include<iostream>
using namespace std;
class Complex {
float real, imag;
public:
void input() {
cout << "Enter real and imaginary parts: ";
cin >> real >> imag;
}
void add(Complex c1, Complex c2) {
real = [Link] + [Link];
imag = [Link] + [Link];
}
void show() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex a, b, c;
[Link]();
[Link]();
[Link](a, b);
cout << "Sum = ";
[Link]();
return 0;
}

10. Implement a program to show private and public visibility modes in a class.

#include<iostream>
using namespace std;
class Demo {
private:
int secret = 10;
public:
void show() {
cout << "Private data accessible inside class: " << secret << endl;
}
};
int main() {
Demo d;
[Link]();
return 0;
}

11. Define a class Account with member functions to input and display account number and
balance.

#include<iostream>
using namespace std;
class Account {
int accno;
float balance;
public:
void input() {
cout << "Enter Account No. and Balance: ";
cin >> accno >> balance;
}
void display() {
cout << "Account No: " << accno << "\nBalance: " << balance << endl;
}
};
int main() {
Account a;
[Link]();
[Link]();
return 0;
}

12. Create a class Book with data members title, author, and price. Use function outside the class
definition using scope resolution operator (::).

#include<iostream>
using namespace std;
class Book {
string title, author;
float price;
public:
void getData();
void showData();
};
void Book :: getData() {
cout << "Enter title, author, and price: ";
cin >> title >> author >> price;
}
void Book :: showData() {
cout << title << " by " << author << " costs " << price << endl;
}
int main() {
Book b;
[Link]();
[Link]();
return 0;
}

13. Create a class Box to demonstrate inline function to calculate volume of a box.

#include<iostream>
using namespace std;
class Box {
int l,b,h;
public:
void input() {
cin >> l >> b >> h;
}
inline int volume() {
return l*b*h;
}
};
int main() {
Box x;
cout << "Enter l, b, h: ";
[Link]();
cout << "Volume = " << [Link]() << endl;
return 0;
}

14. Write a program to create two objects of a class and compare their data members (e.g., age
or marks).

#include<iostream>
using namespace std;
class Student {
int marks;
public:
void input() {
cin >> marks;
}
int getMarks() {
return marks;
}
};
int main() {
Student s1, s2;
cout << "Enter marks of two students: ";
[Link](); [Link]();
if([Link]() > [Link]())
cout << "Student 1 scored higher\n";
else
cout << "Student 2 scored higher\n";
return 0;
}

15. Write a C++ program to define a class Time and add two time objects in hh:mm:ss format.

#include<iostream>
using namespace std;
class Time {
int h,m,s;
public:
void input() {
cin >> h >> m >> s;
}
void add(Time t1, Time t2) {
s = t1.s + t2.s;
m = t1.m + t2.m + s/60;
s %= 60;
h = t1.h + t2.h + m/60;
m %= 60;
}
void display() { cout << h << ":" << m << ":" << s << endl; }
};
int main() {
Time t1, t2, t3;
[Link](); [Link]();
[Link](t1,t2);
[Link]();
return 0;
}

16. Create a class Student with data members marks[5]. Use a loop to input marks and calculate
the average.

#include<iostream>
using namespace std;
class Student {
int marks[5];
public:
void input() {
for(int i=0;i<5;i++)
cin >> marks[i];
}
void average() {
float sum=0;
for(int i=0;i<5;i++) sum+=marks[i];
cout << "Average = " << sum/5 << endl;
}
};
int main() {
Student s;
[Link]();
[Link]();
return 0;
}
17. Demonstrate function call by reference using a class method.

#include<iostream>
using namespace std;
class Number {
public:
void swap(int &a, int &b) {
int temp=a; a=b; b=temp;
}
};
int main() {
int x=10, y=20;
Number n;
[Link](x,y);
cout << "After swap: x="<<x<<" y="<<y<<endl;
return 0;
}

18. Write a program to define a class with a destructor and print a message when the object is
destroyed.

#include<iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Object Created\n";
}
~Demo() {
cout << "Object Destroyed\n";
}
};
int main() {
Demo d;
return 0;
}

19. Define a class Vehicle and inherit a class Car from it to demonstrate single inheritance.

#include<iostream>
using namespace std;
class Vehicle {
public:
void start() {
cout << "Vehicle Started\n";
}
};
class Car : public Vehicle {
public:
void drive() {
cout << "Car Driving\n";
}
};
int main() {
Car c;
[Link]();
[Link]();
return 0;
}

20. Write a program to implement multilevel inheritance with classes Animal → Mammal →
Dog.
#include<iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating\n";
}
};
class Mammal : public Animal {
public:
void walk() {
cout << "Walking\n";
}
};
class Dog : public Mammal {
public:
void bark() {
cout << "Barking\n";
}
};
int main() {
Dog d;
[Link]();
[Link]();
[Link]();
return 0;
}

You might also like