Following is the program for mouse handling in C++ when user left clicks.
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;
int main(){
double start_time = clock();
Beep(500,1000); //BEEP FOR 1 SECOND will be produced. //YOUR CODE HERE.
double end_time = clock();
double time_taken = end_time - start_time;
double time_taken_in_seconds = time_taken/CLOCKS_PER_SEC;
cout << "Time taken during program execution is " << time_taken_in_seconds << " Second(s)"<< endl;
return 0;
}
#include <iostream>
//#include <string>
using namespace std;
class divByZero{}; //An empty CLASS.
int main(){
try{
throw divByZero();
}
catch(divByZero){
cout << "divByZero caught in catch block" << endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////////
class divByZero{
public:
divByZero();
divByZero(string);
string what();
private:
string message;
};
/////////////////////////////////////////////////////////////////////////////////////////////////
divByZero::divByZero(){
message = "Division by Zero IS NOT possbile";
}
/////////////////////////////////////////////////////////////////////////////////////////////////
divByZero::divByZero(string s){
message = s;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
string divByZero::what(){
return message;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
try{
throw divByZero();
}
catch(divByZero s){
cout << "divByZero caught in catch block" << endl;
cout << s.what() << endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////////
class divByZero{
public:
divByZero();
divByZero(string);
string what();
private:
string message;
};
/////////////////////////////////////////////////////////////////////////////////////////////////
divByZero::divByZero(){
message = "Division by Zero IS NOT possbile";
}
/////////////////////////////////////////////////////////////////////////////////////////////////
divByZero::divByZero(string s){
message = s;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
string divByZero::what(){
return message;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void dosomething();
/////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
dosomething();
cout << "Control Comes back in main" << endl;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void dosomething(){
try{
throw divByZero();
cout << "Control Doesn't come back HERE" << endl;
}
catch(divByZero s){
cout << "divByZero caught in catch block" << endl;
cout << s.what() << endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
using namespace std;
ofstream os("linkedList.txt",ios::binary);
struct link{
int data;
link* next;
};
class linkedList{
public:
linkedList();
void addItem(int);
void print(void);
private:
link* first;
};
linkedList::linkedList(){
first = NULL;
}
void linkedList::addItem(int n){
link* temp = new link;
temp->data = n;
temp->next = first;
first = temp;
os.write(reinterpret_cast<char*>(temp),sizeof(temp));
}
void linkedList::print(){
for (link* p = first; p != NULL; p = p->next){
cout << p->data << endl;
}
}
int main(){
int n;
linkedList ll;
do{
cout << "Enter number to insert: (-1 to print): "; cin >> n;
ll.addItem(n);
} while (n != -1);
ll.print();
return 0;
}
1: #include <iostream>
2: using namespace std;
3: #include <string>
4:
5: int main(){
6: int *list[100];
7:
8: try{
9: for (int i = 0; i<100; i++){
10: list[i] = new int[50000000];
11: cout << "Created List[" << i << "] of 50000000 components." << endl;
12: }
13: }
14: catch(bad_alloc be){
15: cout << "In the bad_alloc catch bloc: " << be.what() << endl;
16: }
17: return 0;
18: }
1: #include <iostream>
2: using namespace std;
3: #include <string>
4:
5: int main(){
6: string sentence;
7: string str1, str2, str3;
8:
9: try{
10: sentence = "Testing string exceptions!";
11: cout << "Sentence = " << sentence << endl;
12: cout << "sentence.lenght() = " << static_cast<int>(sentence.length()) << endl;
13: str1 = sentence.substr(8,20);
14: cout << "str1 = " << str1 << endl;
15: str2 = sentence.substr(28,10);
16: cout << "str2 = " << str2 << endl;
17: str3 = "Exception handling. " + sentence;
18: cout << "str3 = " << str3 << endl;
19: cout << "str3.length() = " << str3.length() << endl;
20: }
21:
22: catch (out_of_range re){
23: cout << "In the out_of_range catch block: " << re.what() << endl;
24: }
25: catch(length_error le){
26: cout << "In the length error code block: " << le.what() << endl;
27: }
28:
29: return 0;
30: }
1: #include <iostream>
2: using namespace std;
3: //////////////////////////////////////////////////////////////////////////////////////////////////
4: //////////////////////////////////////////////////////////////////////////////////////////////////
5: template <class TYPE>
6: struct link{//Within this struct definition 'link' means link<TYPE>
7: TYPE data;
8: link* next;
9: };
10: //////////////////////////////////////////////////////////////////////////////////////////////////
11: //////////////////////////////////////////////////////////////////////////////////////////////////
12: template <class TYPE>
13: class ll{//ll => linked list
14: private:
15: link<TYPE> *first;
16: public:
17: ll();
18: void addItem(TYPE);
19: void display();
20: };
21: //////////////////////////////////////////////////////////////////////////////////////////////////
22: template <class TYPE>
23: ll<TYPE>::ll(){
24: first = NULL;
25: }
26: //////////////////////////////////////////////////////////////////////////////////////////////////
27: template <class TYPE>
28: void ll<TYPE>::addItem(TYPE n){
29: link<TYPE>* temp= new link<TYPE>;
30: temp->data = n;
31: temp->next = first;
32: first = temp;
33: }
34: //////////////////////////////////////////////////////////////////////////////////////////////////
35: template <class TYPE>
36: void ll<TYPE>::display(){
37: link<TYPE>* p = first;
38: for (; p!= NULL; p=p->next){
39: cout << p->data << endl;
40: }
41: }
42: //////////////////////////////////////////////////////////////////////////////////////////////////
43: //////////////////////////////////////////////////////////////////////////////////////////////////
44: //template <class TYPE>
45: int main(){
46: ll<char> l;
47: char n;
48: do{
49: cout << "NUMBER: (n to exit)"; cin >> n;
50: l.addItem(n);
51: }
52: while (n!='n');
53: l.display();
54: return 0;
55: }
1: #include <iostream>
2: using namespace std;
3: //////////////////////////////////////////////////////////////////////////////////////////////////
4: //////////////////////////////////////////////////////////////////////////////////////////////////
5: template <class TYPE>
6: struct link{
7: TYPE data;
8: link* next;
9: };
10: //////////////////////////////////////////////////////////////////////////////////////////////////
11: //////////////////////////////////////////////////////////////////////////////////////////////////
12: template <class TYPE>
13: class linkList{
14: private:
15: link<TYPE>* first;
16: public:
17: linkList()
18: { first = NULL; }
19: void addItem(TYPE d);
20: void display();
21: };
22: //////////////////////////////////////////////////////////////////////////////////////////////////
23: //////////////////////////////////////////////////////////////////////////////////////////////////
24: template <class TYPE>
25: void linkList<TYPE>::addItem(TYPE d){
26: link<TYPE> *temp = new link<TYPE>;
27: temp->data = d;
28: temp->next = NULL;
29: if (first == NULL){
30: first = temp;
31: }
32: else{
33: temp->next = first;
34: first = temp;
35: }
36: }
37: //////////////////////////////////////////////////////////////////////////////////////////////////
38: template <class TYPE>
39: void linkList<TYPE>::display(){
40: link<TYPE> *p = first;
41: for (int i = 0; p!=NULL; p = p->next){
42: cout << p->data << endl;
43: }
44: }
45: //////////////////////////////////////////////////////////////////////////////////////////////////
46: //////////////////////////////////////////////////////////////////////////////////////////////////
47: int main(){
48: linkList<double> l;//Here you can write char, int, etc.
49: char opt;
50: do{
51: cout << "Number: "; int n; cin >> n;
52: l.addItem(n);
53: cout << "Do you want to insert any other item? (y/n) "; cin >> opt;
54: }
55: while (opt == 'y');
56: l.display();
57: return 0;
58: }
1: #include <iostream>
2: using namespace std;
3: template <class dT>
4: void swapIt(dT &u, dT &v);
5:
6: int main(){
7: int i1 = 2, i2 = 4;
8: double d1 = 2.2, d2 = 5.6;
9: float f1 = 1.1, f2 = 5.2;
10: char c1 = 'c', c2 = 'R';
11: cout << "i1 = " << i1 << "\ti2 = " << i2 << endl;
12: cout << "d1 = " << d1 << "\td2 = " << d2 << endl;
13: cout << "f1 = " << f1 << "\tf2 = " << f2 << endl;
14: cout << "c1 = " << c1 << "\tc2 = " << c2 << endl;
15:
16: swapIt(i1,i2);
17: swapIt(f1,f2);
18: swapIt(d1,d2);
19: swapIt(c1,c2);
20:
21: cout << "\nAfter swaping: " << endl;
22: cout << "i1 = " << i1 << "\ti2 = " << i2 << endl;
23: cout << "d1 = " << d1 << "\td2 = " << d2 << endl;
24: cout << "f1 = " << f1 << "\tf2 = " << f2 << endl;
25: cout << "c1 = " << c1 << "\tc2 = " << c2 << endl;
26: return 0;
27: }
28: template <class dT>
29: void swapIt(dT &u, dT &v){
30: dT t = u;
31: u = v;
32: v = t;
33: }
1: #include <iostream>
2: #include <fstream>
3: using namespace std;
4:
5: /////////////////////////////////////////////////////////////////////////////////////////////////
6: class person{
7: protected:
8: char name[80];
9: short age;
10: public:
11: void getData(){
12: cout << "Enter name: "; cin.ignore(); cin.getline(name,80,'\n');
13: cout << "Enter age: "; cin >> age;
14: }
15: void showData(){
16: cout << "Name: " << name << endl;
17: cout << "Age: " << age << endl;
18: }
19: };
20: /////////////////////////////////////////////////////////////////////////////////////////////////
21: int main(){
22: person pers;
23: fstream fs("person.dat", ios::in | ios::out | ios::binary);
24: char opt;
25: do{
26: cout << "\nEnter person's data:\n";
27: pers.getData();
28: fs.write(reinterpret_cast<char*>(&pers),sizeof(person));
29: cout << "Any other person left? (y/n) "; cin >> opt;
30: }
31: while (opt == 'y');
32:
33: cout << "\n\nWhich person's data do you want to get? "; int n; cin >> n;
34: fs.seekg(0,ios::end);
35: int endPosition = fs.tellp();
36: int totalPersons = endPosition/sizeof(person);
37: cout << "Total Persons are: " << totalPersons << endl;
38:
39: fs.seekg((n-1)*sizeof(person));
40: fs.read(reinterpret_cast<char*>(&pers),sizeof(person));
41: pers.showData();
42:
43: return 0;
44: }
"Don't let anyone ever make you feel like you don't deserve what you want."