Skip to content

Commit 068b4e7

Browse files
committed
New Programs
1 parent 68fb0fb commit 068b4e7

7 files changed

+429
-0
lines changed

Diff for: Expt_2_1_1.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<iostream>
2+
using namespace std;
3+
class lenght1 //unlike main its not allowed to make variable declarations within member functions/
4+
{
5+
float lenght;
6+
float l1;
7+
public:
8+
lenght1()
9+
{
10+
lenght=100;
11+
l1=50;
12+
}
13+
~lenght1()
14+
{
15+
cout<<"Object Destroyed"<<endl;
16+
}
17+
void setlenght(float l)
18+
{
19+
lenght=l;
20+
}
21+
void convert()
22+
{
23+
if(lenght>12)
24+
{
25+
l1=(int)lenght/12;
26+
lenght=int(lenght)%12; //note that float does not allow mod function to work upon
27+
}
28+
cout<<"Lenght is "<<l1<<" feets and "<<lenght<<" inches"<<endl;
29+
}
30+
};
31+
int main()
32+
{
33+
cout<<"Program to convert Lenght from inches to foot+inches"<<endl;
34+
float l;
35+
cout<<"Enter lenght in inches"<<endl;
36+
cin>>l;
37+
lenght1 obj;
38+
obj.setlenght(l);
39+
obj.convert();
40+
cout<<endl;
41+
return 0;
42+
}

Diff for: Expt_2_1_2.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include<iostream>
2+
using namespace std;
3+
class Queue
4+
{
5+
public: //all public needed to access outside in main
6+
string name;
7+
int roll_number;
8+
double total_marks;
9+
Queue *next;
10+
Queue()
11+
{
12+
name="Nishkarsh";
13+
roll_number=41;
14+
total_marks=256.60;
15+
}
16+
~Queue()
17+
{
18+
cout<<"Object Destroyed"<<endl;
19+
}
20+
void show()
21+
{
22+
cout<<endl;
23+
cout<<"Name of the person is "<<name<<endl<<"Roll number of the person is "<<roll_number<<endl<<"Total marks of the person are "<<total_marks<<endl;
24+
}
25+
}*rear,*front,*node,*ptr,*p;
26+
int main()
27+
{
28+
int quit=0;
29+
cout<<"Hello! This is a program to demonstrate Queue implementation using Classes and Objects"<<endl;
30+
node=new Queue;
31+
cout<<"Enter the name of this person"<<endl;
32+
cin>>node->name;
33+
cout<<"Enter the roll number of this person"<<endl;
34+
cin>>node->roll_number;
35+
cout<<"Enter the total marks of this person"<<endl;
36+
cin>>node->total_marks;
37+
node->next=NULL;
38+
front=node;
39+
rear=node;
40+
node->show();
41+
cout<<endl;
42+
cout<<"Let the party begins"<<endl;
43+
do
44+
{
45+
cout<<"\t\t\t\tOption Menu"<<endl;
46+
cout<<"1) Insert"<<endl;
47+
cout<<"2) Delete"<<endl;
48+
cout<<"3) Show"<<endl;
49+
cout<<"4) Exit"<<endl;
50+
int ch;
51+
cout<<"Enter your choice"<<endl;
52+
cin>>ch;
53+
switch(ch)
54+
{
55+
case 1: cout<<"Insertion at end"<<endl;
56+
p=new Queue;
57+
cout<<"Enter the name of this person"<<endl;
58+
cin>>p->name;
59+
cout<<"Enter the roll number of this person"<<endl;
60+
cin>>p->roll_number;
61+
cout<<"Enter the total marks of this person"<<endl;
62+
cin>>p->total_marks;
63+
rear->next=p;
64+
p->next=NULL;
65+
rear=p;
66+
break;
67+
case 2: cout<<"Deletion at beginning"<<endl;
68+
cout<<"Object to be deleted is"<<endl;
69+
front->show();
70+
ptr=front;
71+
front=front->next;
72+
delete ptr;
73+
break;
74+
case 3: cout<<"Traversal"<<endl;
75+
for(ptr=front;ptr!=NULL;ptr=ptr->next)
76+
{
77+
int i=1;
78+
cout<<"The "<<i++<<" Record is of";
79+
ptr->show();
80+
}
81+
break;
82+
case 4: quit=1;
83+
break;
84+
default: cout<<"Wrong Choice! Try again"<<endl;
85+
}
86+
}while(quit!=1);
87+
return 0;
88+
}

Diff for: Expt_2_2.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<iostream>
2+
using namespace std;
3+
class WWE
4+
{
5+
string name;
6+
float salary;
7+
string nickname;
8+
public:
9+
WWE()
10+
{
11+
name="Seth Rollins";
12+
salary=2000000;
13+
nickname="The Architect";
14+
}
15+
~WWE()
16+
{
17+
cout<<"Object Destroyed"<<endl;
18+
}
19+
WWE(string n,float s,string nn)
20+
{
21+
name=n;
22+
salary=s;
23+
nickname=nn;
24+
}
25+
WWE(WWE &x)
26+
{
27+
name=x.name;
28+
nickname=x.name;
29+
salary=x.salary;
30+
}
31+
void show()
32+
{
33+
cout<<"Name is "<<name<<endl<<"Salary is "<<salary<<endl<<"Nickname is "<<nickname<<endl;
34+
}
35+
};
36+
int main()
37+
{
38+
cout<<"WWE class try out";
39+
WWE a;
40+
a.show();
41+
WWE b("AJ Styles",256643532,"Phenomenal");
42+
b.show();
43+
WWE c=b;
44+
c.show();
45+
return 0;
46+
}
47+

Diff for: Expt_2_3.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include<iostream>
2+
using namespace std;
3+
class Stack
4+
{
5+
public: //all public needed to access outside in main
6+
string name;
7+
int roll_number;
8+
double total_marks;
9+
Stack *next;
10+
void show()
11+
{
12+
cout<<endl;
13+
cout<<"Name of the person is "<<name<<endl<<"Roll number of the person is "<<roll_number<<endl<<"Total marks of the person are "<<total_marks<<endl;
14+
}
15+
}*top,*node,*ptr,*p;
16+
int main()
17+
{
18+
int quit=0;
19+
cout<<"Hello! This is a program to demonstrate Stack implementation using Classes and Objects"<<endl;
20+
node=new Stack;
21+
cout<<"Enter the name of this person"<<endl;
22+
cin>>node->name;
23+
cout<<"Enter the roll number of this person"<<endl;
24+
cin>>node->roll_number;
25+
cout<<"Enter the total marks of this person"<<endl;
26+
cin>>node->total_marks;
27+
node->next=NULL;
28+
top=node;
29+
node->show();
30+
cout<<endl;
31+
cout<<"Let the party begins"<<endl;
32+
do
33+
{
34+
cout<<"\t\t\t\tOption Menu"<<endl;
35+
cout<<"1) Insert"<<endl;
36+
cout<<"2) Delete"<<endl;
37+
cout<<"3) Show"<<endl;
38+
cout<<"4) Exit"<<endl;
39+
int ch;
40+
cout<<"Enter your choice"<<endl;
41+
cin>>ch;
42+
switch(ch)
43+
{
44+
case 1: cout<<"Insertion at end"<<endl;
45+
p=new Stack;
46+
cout<<"Enter the name of this person"<<endl;
47+
cin>>p->name;
48+
cout<<"Enter the roll number of this person"<<endl;
49+
cin>>p->roll_number;
50+
cout<<"Enter the total marks of this person"<<endl;
51+
cin>>p->total_marks;
52+
p->next=top;
53+
top=p;
54+
break;
55+
case 2: cout<<"Deletion at End"<<endl;
56+
cout<<"Object to be deleted is"<<endl;
57+
ptr=top;
58+
ptr->show();
59+
top=top->next;
60+
delete ptr;
61+
break;
62+
case 3: cout<<"Traversal"<<endl;
63+
for(ptr=top;ptr!=NULL;ptr=ptr->next)
64+
{
65+
int i=1;
66+
cout<<"The "<<i++<<" Record is of";
67+
ptr->show();
68+
}
69+
break;
70+
case 4: quit=1;
71+
break;
72+
default: cout<<"Wrong Choice! Try again"<<endl;
73+
}
74+
}while(quit!=1);
75+
return 0;
76+
}

Diff for: single_private.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<iostream>
2+
using namespace std;
3+
class base
4+
{
5+
int a;
6+
protected:
7+
int b;
8+
public:
9+
int c;
10+
void set_data(int a1,int b1, int c1)
11+
{
12+
a=a1; b=b1; c=c1;
13+
}
14+
base()
15+
{
16+
a=10; b=15; c=20;
17+
}
18+
};
19+
class derived: private base
20+
{
21+
int a2;
22+
protected:
23+
int b2;
24+
public:
25+
int c2;
26+
derived()
27+
{
28+
a2=5;
29+
b2=6;
30+
c2=10;
31+
}
32+
void show()
33+
{
34+
//cout<<"Protected Member of base "<<b<<endl;
35+
//cout<<"Public Member of base "<<c<<endl;
36+
cout<<"Private Member of Derived "<<a2<<endl;
37+
cout<<"Protected Member of Derived "<<b2<<endl;
38+
cout<<"Public Member of Derived "<<c2<<endl;
39+
cout<<endl;
40+
}
41+
};
42+
int main()
43+
{
44+
cout<<"Single Level Inheritance with private derivation"<<endl;
45+
derived obj;
46+
obj.show();
47+
//obj.set_data(5,14,16);
48+
//obj.show();
49+
return 0;
50+
}
51+
52+
53+
/* Note that in private derivation all the data members and member functions are inheritable yet all are inaccessible as well. The linux CLI also states that the base is an inaccessible in the child class */
54+
55+
56+
57+

Diff for: single_protected.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<iostream>
2+
using namespace std;
3+
class base
4+
{
5+
int a;
6+
protected:
7+
int b;
8+
public:
9+
int c;
10+
void set_data(int a1,int b1, int c1)
11+
{
12+
a=a1; b=b1; c=c1;
13+
}
14+
base()
15+
{
16+
a=10; b=15; c=20;
17+
}
18+
};
19+
class derived: protected base
20+
{
21+
int a2;
22+
protected:
23+
int b2;
24+
public:
25+
int c2;
26+
derived()
27+
{
28+
a2=5;
29+
b2=6;
30+
c2=10;
31+
}
32+
void show()
33+
{
34+
cout<<"Protected Member of base "<<b<<endl;
35+
cout<<"Public Member of base "<<c<<endl;
36+
cout<<"Private Member of Derived "<<a2<<endl;
37+
cout<<"Protected Member of Derived "<<b2<<endl;
38+
cout<<"Public Member of Derived "<<c2<<endl;
39+
cout<<endl;
40+
}
41+
void show1()
42+
{
43+
set_data(5,6,10);
44+
cout<<"Protected Member of base "<<b<<endl;
45+
cout<<"Public Member of base "<<c<<endl;
46+
cout<<"Private Member of Derived "<<a2<<endl;
47+
cout<<"Protected Member of Derived "<<b2<<endl;
48+
cout<<"Public Member of Derived "<<c2<<endl;
49+
}
50+
};
51+
int main()
52+
{
53+
cout<<"Single Level Inheritance with protected derivation"<<endl;
54+
derived obj;
55+
obj.show();
56+
obj.show1();
57+
return 0;
58+
}
59+
/* Note that in protected derivations public and private members are accessible only as protected. That is they can be accessed within child class but not in main with the object. Thats why here we have called set_data function within the derived class and not in main.
60+
"Protected members are like private for a given class" */
61+
62+
63+
64+

0 commit comments

Comments
 (0)