0% found this document useful (0 votes)
42 views22 pages

Lab Manual 6,7,8

Uploaded by

Manu Harsh T.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views22 pages

Lab Manual 6,7,8

Uploaded by

Manu Harsh T.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

#include<iostream>

using namespace std;


#define SIZE 10
class student
{
private:float m1,m2,m3;
char name[20],num[10];
public:
void getdata();
void dispdata();
};
void student::getdata()
{
cout<<"\nEnter student
USN: " ;
cin>>name;
cout<<"Enter student
name: " ;
cin>>num;
cout<<"Enter student's 3
marks: " ;
cin>>m1>>m2>>m3;
}
void student::dispdata()
{
float avg;
avg=(m1+m2+m3)/3;
cout<<"\n student
USN: "<<name
<<"\n student
name: "<<num
<<"\n student
average: "<<avg;
}
int main()
{
student ob[SIZE];
int n;
cout<<"\n";
cout<<"\
n**************************
*****"
<<"\n Students
Report"
<<"\
n**************************
*****"
<<"\n Enter the
number of students: ";
cin>>n
#include<iostream>
using namespace std;
#define SIZE 10
class student
{
private:float m1,m2,m3;
char name[20],num[10];
public:
void getdata();
void dispdata();
};
void student::getdata()
{
cout<<"\nEnter student
USN: " ;
cin>>name;
cout<<"Enter student
name: " ;
cin>>num;
cout<<"Enter student's 3
marks: " ;
cin>>m1>>m2>>m3;
}
void student::dispdata()
{
float avg;
avg=(m1+m2+m3)/3;
cout<<"\n student
USN: "<<name
<<"\n student
name: "<<num
<<"\n student
average: "<<avg;
}
int main()
{
student ob[SIZE];
int n;
cout<<"\n";
cout<<"\
n**************************
*****"
<<"\n Students
Report"
<<"\
n**************************
*****"
<<"\n Enter the
number of students: ";
cin>>n
#include<iostream>
using namespace std;
#define SIZE 10
class student
{
private:float m1,m2,m3;
char name[20],num[10];
public:
void getdata();
void dispdata();
};
void student::getdata()
{
cout<<"\nEnter student
USN: " ;
cin>>name;
cout<<"Enter student
name: " ;
cin>>num;
cout<<"Enter student's 3
marks: " ;
cin>>m1>>m2>>m3;
}
void student::dispdata()
{
float avg;
avg=(m1+m2+m3)/3;
cout<<"\n student
USN: "<<name
<<"\n student
name: "<<num
<<"\n student
average: "<<avg;
}
int main()
{
student ob[SIZE];
int n;
cout<<"\n";
cout<<"\
n**************************
*****"
<<"\n Students
Report"
<<"\
n**************************
*****"
<<"\n Enter the
number of students: ";
cin>>n
#include<iostream>
using namespace std;
#define SIZE 10
class student
{
private:float m1,m2,m3;
char name[20],num[10];
public:
void getdata();
void dispdata();
};
void student::getdata()
{
cout<<"\nEnter student
USN: " ;
cin>>name;
cout<<"Enter student
name: " ;
cin>>num;
cout<<"Enter student's 3
marks: " ;
cin>>m1>>m2>>m3;
}
void student::dispdata()
{
float avg;
avg=(m1+m2+m3)/3;
cout<<"\n student
USN: "<<name
<<"\n student
name: "<<num
<<"\n student
average: "<<avg;
}
int main()
{
student ob[SIZE];
int n;
cout<<"\n";
cout<<"\
n**************************
*****"
<<"\n Students
Report"
<<"\
n**************************
*****"
<<"\n Enter the
number of students: ";
cin>>n
#include<iostream>
using namespace std;
#define SIZE 10
class student
{
private:float m1,m2,m3;
char name[20],num[10];
public:
void getdata();
void dispdata();
};
void student::getdata()
{
cout<<"\nEnter student
USN: " ;
cin>>name;
cout<<"Enter student
name: " ;
cin>>num;
cout<<"Enter student's 3
marks: " ;
cin>>m1>>m2>>m3;
}
void student::dispdata()
{
float avg;
avg=(m1+m2+m3)/3;
cout<<"\n student
USN: "<<name
<<"\n student
name: "<<num
<<"\n student
average: "<<avg;
}
int main()
{
student ob[SIZE];
int n;
cout<<"\n";
cout<<"\
n**************************
*****"
<<"\n Students
Report"
<<"\
n**************************
*****"
<<"\n Enter the
number of students: ";
cin>>n
for(int i=0;i<n; i++)
{
ob[i].getdata();
}
cout<<"\n-----------------"
<<"\n students Details:"
<<"\n-----------------";
for( int i=0;i<n;i++)
{
cout<<"\n\n
student:"<<i+1
<<"\n ----------";
ob[i].dispdata();
}
return
for(int i=0;i<n; i++)
{
ob[i].getdata();
}
cout<<"\n-----------------"
<<"\n students Details:"
<<"\n-----------------";
for( int i=0;i<n;i++)
{
cout<<"\n\n
student:"<<i+1
<<"\n ----------";
ob[i].dispdata();
}
return
#include<iostream>
using namespace std;
#define SIZE 10
class student
{
private:float m1,m2,m3;
char name[20],num[10];
public:
void getdata();
void dispdata();
6. Write a C++ program to define class name FATHER & SON that holds the income
respectively. Calculate & display total income of a family using Friend function.

#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

Total Income of Family = 7000

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

8. Write a C++ program to explain virtual function (Polymorphism) by creating a base


class polygon which has virtual function areas two classes rectangle & triangle derived
from polygon & they have area to calculate & return the area of rectangle & triangle
respectively.

#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 function for area calculation


virtual float area() const = 0;

// Virtual destructor
virtual ~c_polygon() {}
};

// Derived class Rectangle


class c_rectangle : public c_polygon {
public:
// Override the area function
float area() const override {
return (a * b);
}
};

// Derived class Triangle


class c_triangle : public c_polygon {
public:
// Override the area function
float area() const override {
return (a * b / 2);
}
};

// Function to display area


void displayArea(c_polygon *p) {
p->get_data();
cout << "\nArea: " << p->area() << endl;
}

int main() {
c_rectangle r;
c_triangle t;
c_polygon *p;

// Using base class pointer for Rectangle


p = &r;
cout << "Rectangle:";
displayArea(p);

// Using base class pointer for Triangle


p = &t;
cout << "Triangle:";
displayArea(p);
return 0;
}
OUTPUT:
Rectangle:
Enter two floating values:
2.2
2.2

Area: 4.84
Triangle:
Enter two floating values:
2.2
2.2

Area: 2.42

You might also like