Skip to content

Commit e012e7a

Browse files
committed
add: cpp-programming-ques 06_operator friend 13
1 parent 0292d74 commit e012e7a

File tree

5 files changed

+167
-2
lines changed

5 files changed

+167
-2
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"files.associations": {
3-
"iomanip": "cpp",
4-
"iostream": "cpp"
3+
"new": "cpp",
4+
"iostream": "cpp",
5+
"ostream": "cpp"
56
}
67
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// // Create a student class and overload new and delete operators as a member function of the class.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <conio.h>
6+
#include <string.h>
7+
8+
// // use namespace
9+
using namespace std;
10+
11+
// // define class Student
12+
class Student
13+
{
14+
public:
15+
// // static member variables
16+
static const unsigned int MAX_CHARS_NAME = 31;
17+
18+
private:
19+
// // instance member variables
20+
char name[MAX_CHARS_NAME];
21+
int roll;
22+
23+
public:
24+
// // constructors
25+
Student() {}
26+
27+
Student(char *nm)
28+
{
29+
strcpy(name, nm);
30+
}
31+
32+
Student(int r)
33+
{
34+
roll = r;
35+
}
36+
37+
Student(char *nm, int r)
38+
{
39+
strcpy(name, nm);
40+
roll = r;
41+
}
42+
43+
Student(int r, char *nm)
44+
{
45+
strcpy(name, nm);
46+
roll = r;
47+
}
48+
49+
// // overloaded instance member function to set student's data
50+
void setData(char *nm, int r)
51+
{
52+
strcpy(name, nm);
53+
roll = r;
54+
}
55+
56+
// // overloaded instance member function to set student's data
57+
void setData(int r, char *nm)
58+
{
59+
strcpy(name, nm);
60+
roll = r;
61+
}
62+
63+
// // instance member function to show student's data
64+
void showData()
65+
{
66+
cout << "Name => " << name << ", Roll no. => " << roll;
67+
}
68+
69+
// // instance member function to set roll
70+
void setRoll(int r)
71+
{
72+
roll = r;
73+
}
74+
75+
// // instance member function to set name
76+
void setName(char *nm)
77+
{
78+
strcpy(name, nm);
79+
}
80+
81+
// // instance member function to get roll
82+
int getRoll()
83+
{
84+
return roll;
85+
}
86+
87+
// // instance member function to get name
88+
char *getName(char *nm)
89+
{
90+
strcpy(nm, name);
91+
return nm;
92+
}
93+
94+
// // overload new operator
95+
void *operator new(size_t size)
96+
{
97+
cout << "\n\nOverloaded new operator called with size: " << size << " bytes" << endl;
98+
return ::operator new(size);
99+
}
100+
101+
// // overload new[] operator
102+
void *operator new[](size_t size)
103+
{
104+
cout << "\n\nOverloaded new operator called with size: " << size << " bytes" << endl;
105+
return ::operator new(size);
106+
}
107+
108+
// Overload delete operator
109+
void operator delete(void *ptr)
110+
{
111+
cout << "\n\nOverloaded delete operator called." << endl;
112+
::operator delete(ptr);
113+
}
114+
115+
// Overload delete[] operator
116+
void operator delete[](void *ptr)
117+
{
118+
cout << "\n\nOverloaded delete operator called." << endl;
119+
::operator delete(ptr);
120+
}
121+
};
122+
123+
// // Main Function Start
124+
int main()
125+
{
126+
char name[Student::MAX_CHARS_NAME];
127+
int roll;
128+
129+
// // Get student's Data
130+
cout << "\nEnter Student's Roll Number => ";
131+
cin >> roll;
132+
133+
cout << "\nEnter Student's Name (MAX_CHARACTERS " << Student::MAX_CHARS_NAME - 1 << ") => ";
134+
cin.ignore();
135+
cin.getline(name, Student::MAX_CHARS_NAME);
136+
137+
Student *s1;
138+
s1 = new Student(roll, name); // create object of Student dynamically
139+
140+
// // display students's data
141+
cout << "\n>>>>>>>>>>>> Student's Data <<<<<<<<<<<\n"
142+
<< endl;
143+
s1->showData();
144+
145+
// // deallocate memory of object s1
146+
delete s1;
147+
148+
cout << endl; // Add new line
149+
getch();
150+
return 0;
151+
}
152+
// // Main Function End
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include <malloc.h>
3+
4+
int main()
5+
{
6+
7+
int x;
8+
9+
int *ptr = malloc(4);
10+
11+
printf("\n%d\n", *ptr);
12+
}
39.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)