Read and print details of a student using class program in C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*C++ program to create class for a student.*/
#include <iostream>
using namespace std;
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};
//member function definition, outside of the class
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;
}
//member function definition, outside of the class
void student::putDetails(void){
cout << "Student details:\n";
cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc;
}
int main()
{
student std; //object creation
std.getDetails();
std.putDetails();
return 0;
}
Enter name: mike
Enter roll number: 112
Enter total marks outof 500: 456
Student details:
Name:mike,Roll Number:112,Total:456,Percentage:91.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| /*C++ program to create class for a student.*/#include <iostream>using namespace std;class student{ private: char name[30]; int rollNo; int total; float perc; public: //member function to get student's details void getDetails(void); //member function to print student's details void putDetails(void);};//member function definition, outside of the classvoid 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;}//member function definition, outside of the classvoid student::putDetails(void){ cout << "Student details:\n"; cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc;}int main(){ student std; //object creation std.getDetails(); std.putDetails(); return 0;} |
Adding two distances using class program in C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*C++ program to create a class to read and add two distance. */
#include <iostream>
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 << endl << "\tFeets : " << feet;
cout << endl << "\tInches: " << inch;
}
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 : " << endl;
d1.getDist();
cout << "Enter Distance2 : " << endl;
d2.getDist();
d3 = d1.addDist(d2);
d4 = d1.subDist(d2);
cout << endl << "Distance1 : " ;
d1.showDist();
cout << endl << "Distance2 : " ;
d2.showDist();
cout << endl << "Distance3 : " ;
d3.showDist();
cout << endl << "Distance4 : " ;
d4.showDist();
cout << endl;
return 0;
}
Enter Distance1 :
Enter Value of feets : 10
Enter value of inches : 7
Enter Distance2 :
Enter Value of feets : 15
Enter value of inches : 8
Distance1 :
Feets : 10
Inches: 7
Distance2 :
Feets : 15
Inches: 8
Distance3 :
Feets : 26
Inches: 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| /*C++ program to create a class to read and add two distance. */#include <iostream>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 << endl << "\tFeets : " << feet; cout << endl << "\tInches: " << inch;}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 : " << endl; d1.getDist(); cout << "Enter Distance2 : " << endl; d2.getDist(); d3 = d1.addDist(d2); d4 = d1.subDist(d2); cout << endl << "Distance1 : " ; d1.showDist(); cout << endl << "Distance2 : " ; d2.showDist(); cout << endl << "Distance3 : " ; d3.showDist(); cout << endl << "Distance4 : " ; d4.showDist(); cout << endl; return 0;} |