0% found this document useful (0 votes)
5 views5 pages

Programming For Problem Solving

The document outlines four programming tasks in C++ involving the creation of classes for different scenarios. Task 1 creates a 'Student' class to store a student's name and roll number. Subsequent tasks involve creating a 'Triangle' class to calculate area and perimeter, a 'Complex' class for operations on complex numbers, and another 'Student' class for input and display of student details.
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)
5 views5 pages

Programming For Problem Solving

The document outlines four programming tasks in C++ involving the creation of classes for different scenarios. Task 1 creates a 'Student' class to store a student's name and roll number. Subsequent tasks involve creating a 'Triangle' class to calculate area and perimeter, a 'Complex' class for operations on complex numbers, and another 'Student' class for input and display of student details.
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/ 5

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>

using namespace std;

class Triangle{

private:

int s, a, b, c, A, P;

public:

void area(){

cout<<"Enter Sides of triangle: ";

cin>>a>>b>>c;

s=(a+b+c)/2;

A= sqrt(s*(s-a)*(s-b)*(s-c));

cout<<"Area of Triangle ("<<a<<","<<b<<","<<c<<")"<<" : "<<A;

void perimeter(){

P=a+b+c;

cout<<"\nand Perimeter: "<<P;

};
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>

using namespace std;

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);

cout<<"\nand Difference = "<<Differ1<<"+i"<<Differ2; }

};

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>

using namespace std;

class student{

private:

char name[20];

int roll_no;

char branch[20];
public:

void getData(){

cout<<"Give the details of Student (name, roll no, branch): ";

cin>>name>>roll_no>>branch;

void display(){

cout<<"The details of Student are\nName: "<<name;

cout<<"\nRoll no: "<<roll_no;

cout<<"\nBranch: "<<branch;

};

int main()

student S1;

S1.getData();

S1.display();

return 0;

You might also like