Skip to content

Commit ca101e8

Browse files
committed
add: cpp-programming-ques 06_operator friend 09
1 parent 4a0750c commit ca101e8

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}

06_operator_overloading_and_friend_function/06_concatenation_compare_strings.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ class CString
6666
return s;
6767
}
6868

69+
// // instance member function print string
70+
void printString()
71+
{
72+
cout << str;
73+
}
74+
6975
// // overload binary addition operator (+) to concate two strings
7076
CString operator+(CString str1)
7177
{

06_operator_overloading_and_friend_function/08_matrix_negation.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Overload the - (Unary) should negate the numbers stored in the object.
1212
// // Header files
1313
#include <iostream>
1414
#include <conio.h>
15-
#include <stdlib.h>
1615
#include <iomanip>
1716

1817
// // use namespace
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
3+
Consider the following class mystring
4+
Class CString
5+
{
6+
char str [100];
7+
};
8+
Overload operator “!” to reverse the case of each alphabet in the string (Uppercase to Lowercase and vice versa).
9+
10+
*/
11+
12+
// // Header files
13+
#include <iostream>
14+
#include <conio.h>
15+
#include <string.h>
16+
17+
// // use namepsace
18+
using namespace std;
19+
20+
// // define class CString
21+
class CString
22+
{
23+
24+
public:
25+
// // static member variable
26+
static const int MAX_CHARS = 101;
27+
28+
private:
29+
// // instance member variables
30+
char str[MAX_CHARS];
31+
int length;
32+
33+
public:
34+
// // constructors
35+
CString()
36+
{
37+
length = 0;
38+
}
39+
40+
CString(const char *s)
41+
{
42+
strcpy(str, s);
43+
length = strlen(str);
44+
}
45+
46+
CString(char *s)
47+
{
48+
strcpy(str, s);
49+
length = strlen(str);
50+
}
51+
52+
// // overloaded instance member function to set string
53+
void setString(const char *s)
54+
{
55+
strcpy(str, s);
56+
length = strlen(str);
57+
}
58+
59+
// // overloaded instance member function to set string
60+
void setString(char *s)
61+
{
62+
strcpy(str, s);
63+
}
64+
65+
// // instance member function to get string
66+
char *getString(char *s)
67+
{
68+
strcpy(s, str);
69+
return s;
70+
}
71+
72+
// // instance member function print string
73+
void printString()
74+
{
75+
cout << str;
76+
}
77+
78+
// // overload logical not (!) operator
79+
CString operator!()
80+
{
81+
CString temp;
82+
int i;
83+
for (i = 0; str[i]; i++)
84+
{
85+
if (str[i] >= 'A' && str[i] <= 'Z')
86+
temp.str[i] = str[i] + 32;
87+
else if (str[i] >= 'a' && str[i] <= 'z')
88+
temp.str[i] = str[i] - 32;
89+
else
90+
temp.str[i] = str[i];
91+
}
92+
temp.str[i] = 0; // add null character to terminate string
93+
94+
return temp;
95+
}
96+
};
97+
98+
// // Main Function Start
99+
int main()
100+
{
101+
char str1[CString::MAX_CHARS], str2[CString::MAX_CHARS], str3[CString::MAX_CHARS];
102+
103+
// // Get string
104+
cout << "\nEnter Any String (s1) (MAX_CHARACTERS " << CString::MAX_CHARS << ") => ";
105+
cin.getline(str1, CString::MAX_CHARS);
106+
107+
CString s1(str1), s2; // create objects of CString class
108+
109+
cout << "\nString => ";
110+
s1.printString();
111+
112+
// // perform operation of string
113+
s2 = !s1;
114+
115+
cout << "\nResult After Performing (s2 = !s1) Operation On String => ";
116+
s2.printString();
117+
118+
cout << endl; // Add new line
119+
getch();
120+
return 0;
121+
}
122+
// // Main Function End
Binary file not shown.

0 commit comments

Comments
 (0)