Experiment 09
AIM: Programming using object-oriented programming concepts (using data members and member functions)
Task 1: Create a class named 'Student' with a string (char array) variable 'name' and an integer variable 'roll_no'. Assign the value of roll_no as 2 and that of name as "John" in main( ) function by creating an object of the class Student and display the same.
#include <iostream>
using namespace std;
#include <cstring>
class Student
{
public:
char name[5];
int roll_no;
};
int main()
{
Student s;
s.roll_no = 2;
strcpy(s.name, "John");
cout << "Student name: " << s.name << endl << "Student Roll no.: " << s.roll_no;
return 0;
}
Task 2: Create a class student having data members name, rollno & branch and define two member function getData( ) & display( ) for taking input & display the same. Write a complete C++ code for displaying the information of a student.
#include <iostream>
using namespace std;
class student
{
private:
char name[10];
int roll_no;
char branch[5];
public:
void getData()
{
cout << "Enter the Name Roll no. & Branch of the Student:" << endl;
cin >> name >> roll_no >> branch;
}
void display()
{
cout << "Student Name: " << name << endl << "Roll no: " << roll_no << endl << "Branch:" << branch << endl;
}
};
int main()
{
student s;
s.getData();
s.display();
return 0;
}
Task 3: Create a student record (name, rollno, marks of 3 subjects and score), calculate the average, store average in a score data member. If score<40, declare FAIL else PASS along with student details, maintain 10 students records. (make use of member function to read and display records)
#include <iostream>
using namespace std;
class student
{
private:
char name[10];
int roll_no;
int ms1;
int ms2;
int ms3;
float score;
public:
void read()
{
cout << "Enter the Name, Roll no & marks of the three subjects:" << endl;
cin >> name >> roll_no >> ms1 >> ms2 >> ms3;
}
void display()
{
score = (ms1 + ms2 + ms3) / 3;
cout << "Student Name: " << name << endl
<< "Roll no: " << roll_no << endl
<< "Marks in Sub 1: " << ms1 << endl
<< "Marks in Sub 2: " << ms2 << endl
<< "Marks in Sub 3: " << ms3 << endl
<< "Score: " << score << endl;
if (score < 40)
cout << "FAIL" << endl;
else
cout << "PASS" << endl;
}
};
int main()
{
student s[10];
for (int i = 0; i < 10; i++)
{
s[i].read();
s[i].display();
}
return 0;
}
Task 4: Write a program to overload sum function to perform addition of two integers, three integers and two floating-point numbers.
#include <iostream>
using namespace std;
int sum(int x, int y)
{
int s = x + y;
return s;
}
int sum(int x, int y, int z)
{
int s = x + y + z;
return s;
}
double sum(double x, double y)
{
double s = x + y;
return s;
}
int main()
{
cout << "Sum of two integers: " << sum(10, 20) << endl;
cout << "Sum of three integers: " << sum(30, 40, 50) << endl;
cout << "Sum of two floats: " << sum(16.12, 20.05);
return 0;
}
Task 5: Create a class named “Shapes” with data member area. Write a member function “calArea” with two float parameters to calculate the area of rectangle and overload the same function having one float parameter to calculate the area of square.
#include <iostream>
using namespace std;
class Shapes
{
private:
float area;
public:
void calArea(float x, float y)
{
area = x * y;
}
void calArea(float x)
{
area = x * x;
}
void display()
{
cout << area << endl;
}
};
int main()
{
Shapes s1;
s1.calArea(16.12, 20.05);
cout << "Area of rectangle: ";
s1.display();
Shapes s2;
s2.calArea(47.69);
cout << "Area of Square: ";
s2.display();
return 0;
}