Lab Manual 6,7,8
Lab Manual 6,7,8
#include <iostream>
using namespace std;
class father
{
private:
int income_father;
public:
void read();
void show();
friend class son;
};
class son
{
private:
int income_son;
public:
void read();
void show();
void add (father A)
{
int t;
t=A.income_father+income_son;
cout<<"\n Total Income of Family = "<<t<<endl;
}
};
void son::read()
{
cout<<"Enter income of son";
cin>>income_son;
}
void son::show()
{
cout<<"son income= "<<income_son<<endl;
}
void father::read()
{
cout<<"Enter father income ";
cin>>income_father;
}
void father::show()
{
cout<<"father income = "<<income_father<<endl;
}
int main()
{
father f;
son s;
f.read();
s.read();
f.show();
s.show();
s.add(f);
return 0;
}
OUTPUT:
Enter father income 2000
Enter income of son 5000
father income = 2000
son income= 5000
7. Write a C++ program to accept the student detail such as name & 3 different marks
by get_data() method & display the name & average of marks using display() method.
Define a friend function for calculating the average marks using the method
mark_avg().
#include <iostream>
using namespace std;
class Student; // Forward declaration for friend function
class Student {
private:
string name;
int mark1, mark2, mark3;
public:
// Method to accept student details
void get_data() {
cout << "Enter student name: ";
cin>>name;
cout << "Enter three marks: ";
cin >> mark1 >> mark2 >> mark3;
}
// Friend function to calculate average marks
friend float mark_avg( Student student);
// Method to display student name and average marks
void display() {
float avg = mark_avg(*this);
cout << "Student Name: " << name << endl;
cout << "Average Marks: " << avg << endl;
}
};
/ Friend function to calculate average marks
float mark_avg(Student student) {
return (student.mark1 + student.mark2 + student.mark3) / 3.0;
}
int main() {
Student student;
student.get_data();
student.display();
return 0;
}
OUTPUT:
Enter student name: Bimbitha
Enter three marks: 21 32 34
Student Name: Bimbitha
Average Marks: 29
#include<iostream>
using namespace std;
class c_polygon
{
protected:
float a,b;
public:
void get_data(
#include <iostream>
using namespace std;
// Base class
class c_polygon {
protected:
float a, b;
public:
// Method to get data
void get_data() {
cout << "\nEnter two floating values:\n";
cin >> a >> b;
}
// Virtual destructor
virtual ~c_polygon() {}
};
int main() {
c_rectangle r;
c_triangle t;
c_polygon *p;
Area: 4.84
Triangle:
Enter two floating values:
2.2
2.2
Area: 2.42