Ktu notes
Enviado por Jithosh Babu • 19 de Abril de 2020 • Apuntes • 2.527 Palabras (11 Páginas) • 156 Visitas
CLASS
1.Program to print helloworld
/* C++ program to create a simple class and object.*/
#include
using namespace std;
class Hello
{
public:
void sayHello()
{
cout<< "Hello World" <
}
};
int main()
{
Hello h;
h.sayHello();
return 0;
}
2. To add two distances
/*C++ program to create a class to read and add two distance. */
#include
using namespace std;
class Distance
{
private:
int feet;
int inch;
public:
Distance (); //Constructor
void getDist();
void showDist();
Distance addDist( Distance d2 );
Distance subDist( Distance d2 );
};
Distance:: Distance ()
{
feet = 0; inch = 0;
}
void Distance:: getDist()
{
cout<< "Enter Value of feets : "; cin>> feet;
cout<< "Enter value of inches : "; cin>> inch;
inch = (inch >= 12) ? 12 : inch;
}
void Distance:: showDist()
{
cout<
cout<
}
Distance Distance::addDist( Distance d2 )
{
Distance temp;
temp.feet = feet + d2.feet;
temp.inch = inch + d2.inch;
if(temp.inch>= 12)
{
temp.feet++;
temp.inch -= 12;
}
return temp;
}
Distance Distance::subDist( Distance d2 )
{
Distance temp;
temp.feet = feet - d2.feet;
temp.inch = inch - d2.inch;
if(temp.inch< 0 )
{
temp.feet--;
temp.inch = 12 + temp.inch;
}
return temp;
}
int main()
{
Distance d1;
Distance d2;
Distance d3;
Distance d4;
cout<< "Enter Distance1 : " <
d1.getDist();
cout<< "Enter Distance2 : " <
d2.getDist();
d3 = d1.addDist(d2);
d4 = d1.subDist(d2);
cout<
d1.showDist();
cout<
d2.showDist();
cout<
d3.showDist();
cout<
d4.showDist();
cout<
return 0;
}
3. To read and display the details of N students
/*C++ program to read and print N students’ details using class */
#include
using namespace std;
#define MAX 100
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
voidgetDetails(void);
voidputDetails(void);
};
void student::getDetails(void)
{
cout<< "Enter name: " ;
cin>> name;
cout<< "Enter roll number: ";
cin>>rollNo;
cout<< "Enter total marks outof 500: ";
cin>> total;
perc=(float)total/500*100;
}
void student::putDetails(void)
{
cout<< "Student details:\n";
cout<< "Name:"<< name << ",Roll Number:" <
}
int main()
{
int n,i;
Student s[MAX];
cout<< "Enter total number of students: ";
cin>> n;
for(i=0;i< n; i++)
{
cout<< "Enter details of student " << i+1 << ":\n";
s[i].getDetails();
}
cout<
for(i=0;i< n; i++)
{
cout<< "Details of student " << (i+1) << ":\n";
std[i].putDetails();
}
return 0;
}
4. Convert time to seconds
/*C++ program to read time in HH:MM:SS format and convert into total seconds.*/
#include
#include
using namespace std;
class Time
{
int seconds;
inthh,mm,ss;
public:
void getTime(void);
void convertIntoSeconds(void);
void displayTime(void);
};
void Time::getTime(void)
{
cout<< "Enter time:" <
cout<< "Hours? ";
cin>>hh;
cout<< "Minutes? ";
cin>> mm;
cout<< "Seconds? ";
...