Programming For Problem Solving
Programming For Problem Solving
EXPERIMENT – 10
Task 1: Create a class named 'Student' with a string variable
'name' and an integer variable 'roll_no'. Assign the value of
roll_no as '2' and that of name as "John" by creating an object of
the class Student.
PROGRAM:
#include <iostream>
#include <string>
using namespace std;
class student {
public:
string name;
int roll_no;
};
int main() {
student st1;
st1.name = "john";
st1.roll_no = 2;
cout << "Name: " << st1.name << endl;
cout << "roll: " << st1.roll_no << endl;
}
Task 2: Write a program to print the area and perimeter of a triangle
having sides of 3, 4 and 5 units by creating a class named 'Triangle' with a
function to print the area and perimeter.
PROGRAM:
#include <iostream>
#include <math.h>
class Triangle{
private:
int s, a, b, c, A, P;
public:
void area(){
cin>>a>>b>>c;
s=(a+b+c)/2;
A= sqrt(s*(s-a)*(s-b)*(s-c));
void perimeter(){
P=a+b+c;
};
int main()
Triangle T1;
T1.area();
T1.perimeter();
return 0;
Task 3: Print the sum and difference of two complex numbers by creating a
class named 'Complex' with separate functions for each operation whose
real and imaginary parts are entered by the user.
Program:
#include <iostream>
class Class{
private:
int a,b,c,d,Sum1,Sum2,Differ1,Differ2;
public:
void sum(){
cout<<"Enter the Real and Imaginary part of 1st complex no: ";
cin>>a>>b;
cout<<"Enter the Real and Imaginary part of 2nd complex no: ";
cin>>c>>d;
Sum1=(a+c);
Sum2=(b+d);
cout<<"Sum of ("<<a<<"+i"<<b<<") & ("<<c<<"+i"<<d<<")"<<" =
"<<Sum1<<"+i"<<Sum2; }
void difference(){
Differ1=(a-c);
Differ2=(b-d);
};
int main(){
Class T1;
T1.sum();
T1.difference();
return 0;
Task 4: Create a class student having data members name, rollno &
branch of student. Also declare two methods i.e. getData( ) & display( ) for
taking input & display the same. Write a complete C++ code for display the
information of a single student.
Program:
#include <iostream>
class student{
private:
char name[20];
int roll_no;
char branch[20];
public:
void getData(){
cin>>name>>roll_no>>branch;
void display(){
cout<<"\nBranch: "<<branch;
};
int main()
student S1;
S1.getData();
S1.display();
return 0;