0% found this document useful (0 votes)
55 views

C++ Assignment

The document contains 5 code snippets that demonstrate C++ classes and objects. The snippets show: 1) Defining a student class with private data members and public member functions to get and display student details 2) Defining an array of student objects and using a for loop to get details from multiple students 3) Defining a Calculator class to demonstrate a class performing math operations on data members 4) Defining a candidate class to model an election problem and using arrays to track votes and display results 5) Defining a time class to convert seconds to hours, minutes, seconds format

Uploaded by

Kavibharath R
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)
55 views

C++ Assignment

The document contains 5 code snippets that demonstrate C++ classes and objects. The snippets show: 1) Defining a student class with private data members and public member functions to get and display student details 2) Defining an array of student objects and using a for loop to get details from multiple students 3) Defining a Calculator class to demonstrate a class performing math operations on data members 4) Defining a candidate class to model an election problem and using arrays to track votes and display results 5) Defining a time class to convert seconds to hours, minutes, seconds format

Uploaded by

Kavibharath R
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/ 13

C++ ASSIGNMENT

1.
#include <iostream>
using namespace std;
class student
{
private:
char name[30];
int roll.no;
int total;
float perc;
public:
void getDetails(void);
void putDetails(void);
};
void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> roll.no;
cout << "Enter total marks out of 600: ";
cin >> total;

perc=(float)total/600*100;
}
void student::putDetails(void)
{
cout << "Student details:\n";
cout << "Name:"<< name <<endl;
cout<< "Roll Number:" << roll.no << endl;
cout<<"Total:" << total << endl;
cout<<"Percentage:" << perc;
}

int main()
{
student stu1;
stu1.getDetails();
stu1.putDetails();
return 0;
}

Output:
Enter name: Kavi
Enter roll number: 1901102
Enter total marks out of 600: 480
Student details:
Name: Kavi
Roll Number: 1901102
Total: 480
Percentage: 80
2.
#include<iostream>
using namespace std;
#define a 10
class student
{
private:
char name[30];
int rollno;
int total;
float perc;
public:
void getdetails(void);
void putdetails(void);
};
void student::getdetails(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter roll number:";
cin>>rollno;
cout<<"Enter total marks out of 600:";
cin>>total;
perc=(float)total/600*100;
}
void student::putdetails(void)
{
cout<<"Student details:\n";
cout<<"Name:"<<name<<endl;
cout<<"Roll number:"<<rollno<<endl;
cout<<"Total:"<<total<<endl;
cout<<"Percentage:"<<perc;
}
int main()
{
student std[a];
int n,i;
cout<<"Enter total number of students:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the details of student"<<i+1<<":\n";
std[i].getdetails();
}
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"Details of students"<<(i+1)<<":\n";
std[i].putdetails();
}
return 0;
}
Output:
Enter the total number of students: 2
Enter details of student 1:
Enter name: Kavi
Enter roll number: 2
Enter total marks out of 600:500
Enter details of student 2:
Enter name: Mike
Enter roll number: 13
Enter total marks out of 600:480
Details of student 1:
Student details:
Name: Kavi
Roll number: 2
Total: 500
Percentage: 83.333
Student details:
Name: Mike
Roll number: 13
Total: 480
Percentage: 80
3.
#include <iostream>
using namespace std;
class Calculator
{
private:
T num1, num2;
public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}
void displayResult( )
{
cout << "Numbers are: " << num1 << " and " << num2 << endl;
cout << "Addition is: " << add( ) << endl;
cout << "Subtraction is: " << subtract( ) << endl;
cout << "Product is: " << multiply( ) << endl;
cout << "Division is: " << divide( ) << endl;
}
T add( )
{
return num1 + num2;
}

T subtract( )
{
return num1 - num2;
}
T multiply( )
{
return num1 * num2;
}
T divide( )
{
return num1 / num2;
}
};
int main()
{
Calculator intCalc(2, 1);
cout << "Results:" << endl;
intCalc.displayResult();
return 0;
}
Output:
Results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2
4.
#include <iostream>
using namespace std;
class candidate
{
public:
string name;
int vote;
};
void outputElection(candidate* arr)
{
int total_vote=0;
for(int i=0;i<5;i++)
{
total_vote=total_vote+arr[i].vote;
}
cout<<"Result of the election............."<<endl;
cout<<"Name of candidate"<<"\t"<<"Vote received"
<<"\t"<<"Percentage"<<endl;
for(int i=0;i<5;i++)
{
cout<<arr[i].name<<"\t\t\t";
cout<<arr[i].vote<<"\t\t";
cout<<(arr[i].vote*100)/total_vote<<"%"<<endl;
}

int max=INT_MIN,count=0;
int index[5]={0};
for(int i=0;i<5;i++)
{
if(arr[i].vote>max)
{
max=arr[i].vote;
}
}
for(int i=0;i<5;i++)
{
if(arr[i].vote==max)
{
index[count]=i;
count++;
}
}
if(count==1)
cout<<"The winner is "<<arr[index[count-1]].name<<endl;
else
{
cout<<"There is tie between:"<<endl;
for(int i=0;i<count-1;i++)
cout<<arr[index[i]].name<<", ";
cout<<arr[index[count-1]].name<<endl;
cout<<"All are winner\n";
}
return ;
}
int main()
{
string s;
int v;
candidate arr[5];
cout<<"Enter candidates last name, there are five candidates\n";
for(int i=0;i<5;i++)
{
cout<<"Enter candidate "<<i<<" last name\n";
cin>>s;
arr[i].name=s;
cout<<"Enter no of votes received by candidate "<<i<<endl;
cin>>v;
arr[i].vote=v;
}
outputElection(arr);
return 0;
}
Output:
Enter candidates last name, there are five candidates
Enter candidate 0 last name
Peter
Enter no of votes received by candidate 0
30
Enter candidate 1 last name
Roy
Enter no of votes received by candidate 1
20
Enter candidate 2 last name
Ali
Enter no of votes received by candidate 2
40
Enter candidate 3 last name
Hales
Enter no of votes received by candidate 3
60
Enter candidate 4 last name
John
Enter no of votes received by candidate 4
10
Result of the election.............
Name of candidate Vote received Percentage
Peter 30 18%
Roy 20 12%
Ali 40 25%
Hales 60 37%
John 10 6%
The winner is Hales
5.
#include <iostream>
using namespace std;
class time
{
Private:
int time = 0;
int hour = 0;
int min = 0;
int sec = 0;
public:
void gettime(void);
};
void time:: gettime(void)
{
cout << "Enter a time in seconds: ";
cin >> time;
hour = time/3600;
time = time%3600;
min = time/60;
time = time%60;
sec = time;
cout<<"\nThe time in HH:MM:SS format is: "<<hour<<" hours "
<<min<<" minutes and "<<sec<<" seconds\n";
}
int main()
{
time T;
T.gettime();
return 0;
}
Output:
Enter time in seconds: 3666
The time format in HH:MM:SS is: 1 hours 1 minutes and 6 seconds

You might also like