Skip to content

Commit b2fb3f7

Browse files
committed
classes in cpp
0 parents  commit b2fb3f7

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed

Student1.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <string.h>
3+
using namespace std;
4+
5+
class Student1
6+
{
7+
private:
8+
char name[40];
9+
int mks[4], total;
10+
11+
public:
12+
void accept();
13+
void display();
14+
15+
Student1()
16+
{
17+
strcpy(name, "\0");
18+
for (int i = 0; i < 4; i++)
19+
mks[i] = 0;
20+
total=0;
21+
}
22+
};
23+
24+
void Student1::accept()
25+
{
26+
char nm[2];
27+
cout << "\nEnter your name: ";
28+
// gets(name);
29+
cin.getline(name, 40);
30+
cout << "\nEnter the marks of 4 subjects: \n";
31+
for (int i = 0; i < 4; i++)
32+
{
33+
cin >> mks[i];
34+
total += mks[i];
35+
}
36+
cin.getline(nm, 2);
37+
}
38+
39+
void Student1::display()
40+
{
41+
cout << "\n Name : " << name;
42+
cout << "\n Marks Obtained : ";
43+
for (int i = 0; i < 4; i++)
44+
cout << mks[i] << ",";
45+
cout << "\n Total marks = "<< total;
46+
}
47+
int main()
48+
{
49+
Student1 s[3];
50+
int i = 0;
51+
for (i = 0; i < 3; i++)
52+
{
53+
s[i].accept();
54+
printf("-----------------------------------------------------");
55+
}
56+
57+
for (i = 0; i < 3; i++)
58+
{
59+
s[i].display();
60+
printf("\n");
61+
}
62+
return 0;
63+
}

Student1.exe

45.1 KB
Binary file not shown.

Student2.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Student2
5+
{
6+
private:
7+
char name[100];
8+
double marks[3];
9+
double total;
10+
11+
public:
12+
void display();
13+
14+
Student2()
15+
{
16+
cout << "\nEnter name of Student: ";
17+
cin >> name;
18+
for (int i = 0; i < 3; i++)
19+
{
20+
cout << "Enter marks of Subject " << i + 1 << " : ";
21+
cin >> marks[i];
22+
}
23+
total = 0.0;
24+
}
25+
26+
Student2(double mrks[3])
27+
{
28+
cout << "\nEnter name of Student: ";
29+
cin >> name;
30+
for (int i = 0; i < 3; i++)
31+
{
32+
marks[i] = mrks[i];
33+
}
34+
}
35+
36+
};
37+
38+
void Student2::display()
39+
{
40+
cout << "\nStudent Name : " << name;
41+
for (int i = 0; i < 3; i++)
42+
{
43+
total += marks[i];
44+
}
45+
cout << "\nTotal marks = " << total << endl;
46+
}
47+
48+
int main()
49+
{
50+
double mr[] = {80, 90, 100};
51+
Student2 s1(mr);
52+
s1.display();
53+
54+
cout << "\n------------------------------------------";
55+
56+
Student2 s2;
57+
s2.display();
58+
59+
return 0;
60+
}

Student2.exe

46.2 KB
Binary file not shown.

product.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//product name, quantity, code, price, instock, outstock;
2+
#include <iostream>
3+
using namespace std;
4+
5+
class product
6+
{
7+
private:
8+
char name[100];
9+
long code;
10+
double price;
11+
int stocks, newstk;
12+
int nitems;
13+
14+
public:
15+
void accept();
16+
void display();
17+
void instock();
18+
void outstock();
19+
};
20+
21+
void product::accept()
22+
{
23+
cout << "\nEnter product name: ";
24+
gets(name);
25+
cout << "Enter product code: ";
26+
cin >> code;
27+
cout << "Enter product price: Rs. ";
28+
cin >> price;
29+
}
30+
31+
void product::display()
32+
{
33+
cout << "\nProduct Name : " << name;
34+
cout << "\nCode : " << code;
35+
cout << "\nPrice : Rs. " << price;
36+
}
37+
38+
void product::instock()
39+
{
40+
stocks=0;
41+
cout << "Enter number of new stocks: ";
42+
cin >> newstk;
43+
stocks += newstk;
44+
}
45+
46+
void product::outstock()
47+
{
48+
char ch;
49+
display();
50+
cout << "\nEnter no. of items required for customer : ";
51+
cin >> nitems;
52+
53+
if (stocks >= nitems)
54+
{
55+
stocks -= nitems;
56+
cout << "In stock !";
57+
cout << "\nNo. of products left: " << stocks << endl;
58+
}
59+
else
60+
{
61+
cout << "\nOut of stock!" << endl;
62+
cout << "\nNo. of products left: " << stocks << endl;
63+
}
64+
cout << "\n---------------------------------------------------------------------\n";
65+
cout << "\nDo you want to continue for next customer (Press Y -YES or N -NO): ";
66+
cin >> ch;
67+
if (ch == 'Y' || ch == 'y')
68+
{
69+
instock();
70+
outstock();
71+
}
72+
else
73+
exit(0);
74+
}
75+
76+
int main()
77+
{
78+
product p;
79+
p.accept();
80+
p.instock();
81+
p.outstock();
82+
return 0;
83+
}

product.exe

47.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)