Ktu notes
Jithosh BabuApuntes19 de Abril de 2020
2.527 Palabras (11 Páginas)190 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? ";
cin>>ss;
}
void Time::convertIntoSeconds(void)
{
seconds = hh*3600 + mm*60 + ss;
}
void Time::displayTime(void)
{
cout<< "The time is = " <
cout<< "Time in total seconds: " << seconds;
}
int main()
{
Time T;
T.getTime();
T.convertIntoSeconds();
T.displayTime();
return 0;
}
5. Roots of a quadratic equation
#include
#include
class equation
{
float a,b,c;
public:
void getinfo(float a, float b,float c );
void display( );
void equal(float a, float b);
void imag( );
void real(float a,floatb,float c);
};
void equation :: getinfo(float aa,floatbb,float cc)
{
a=aa;
b=bb;
c=cc;
}
void equation::display( )
{
cout<
cout<<”b=”<
cout<<”c=”<
}
void equation ::equal (float a,float b)
{
float x;
x = -b/(2*a);
cout<<”Roots are equal “<
}
void equation :: imag( )
...