Assignment-2: 1. Write A Program To Demonstrate The Working of Scope Resolution Operator in C++

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

ASSIGNMENT-2

1. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF SCOPE


RESOLUTION OPERATOR IN C++.

//Demonstration of working of SCOPE RESOLUTION OPERATOR....

#include<iostream>
using namespace std;
int a=10; //global variable (accessible to all functions)
class Scope
{
public:
void display(); //Prototype of display function
};
void Scope::display() //Defining function using Scope Resolution Operator
{
cout<<"Function defining outside the class using scope resolution operator..\n";
}
int main()
{
int a=100; // local variable (accessible only in main function)
Scope s;
s.display(); //calling function
cout<<"Value outside the main function is:"<<::a<<endl; // Using scope resolution operator
cout<<"Value inside the main function is:"<<a<<endl;
return 0;
}
2. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF MEMBER
FUNCTION IN C++.

//Demonstration of the working of MEMBER FUNCTION....

#include<iostream>
using namespace std;
class Member
{
public:
double l,b,h;
public:
//Member function declaration
double getvolume(void);
void setlength(double len);
void setbreadth(double bre);
void setheight(double hei);
};
//Member function definitions
double Member::getvolume()
{
return l*b*h;
}
void Member::setlength(double len)
{
l=len;
}
void Member::setbreadth(double bre)
{
b=bre;
}
void Member::setheight(double hei)
{
h=hei;
}
//Main function of the program
int main()
{
double volume; //Store the volume of box here
Member box1,box2; // Declare Box1,box2 of type Member
// box1 specification
box1.setlength(2.0);
box1.setbreadth(3.5);
box1.setheight(1.4);
// box2 specification
box2.setlength(3.4);
box2.setbreadth(2.9);
box2.setheight(1.4);

volume=box1.getvolume();//Volume of box1
cout<<"Volume of 1st box is:"<<volume<<"\n";
volume=box2.getvolume();//Volume of box2
cout<<"Volume of 2nd box is:"<<volume<<"\n";
return 0;
}

3. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF NESTING OF


MEMBER FUNCTION IN C++.
//Demonstration of the working of NESTING OF MEMBER FUNCTION....

#include<iostream>
using namespace std;
class Add
{
int a,b;
public:
//Declaration of member function
void getdata();
int sum();
void putdata();
};
//Member function definition outside the class
void Add::getdata()
{
cout<<"Enter 2 numbers to add:\n";
cin>>a>>b;
putdata(); //Nesting of member function
}
int Add::sum()
{
int z;
z=a+b;
return z;
}
void Add::putdata()
{
cout<<"Sum is:"<<sum(); //Nesting of member function
}
//Main function of program
int main()
{
Add a;
a.getdata(); //calling member function
return 0;
}

4. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF INLINE


FUNCTION IN C++.
//Demonstration of the working of INLINE FUNCTION..

#include<iostream>
using namespace std;
class operation
{
private:
int a,b,sum,sub,mul;
float div;
public:
//Declaration of member function
void get();
void addition();
void subtraction();
void multiplication();
void division();
};
//definition of member function
inline void operation::get() //using inline prefix
{
cout<<"Enter two values for operation:\n";
cin>>a>>b;
}
inline void operation::addition() //using inline prefix
{
sum=a+b;
cout<<"Addition is:"<<sum<<endl;
}
inline void operation::subtraction() //using inline prefix
{
sub=a-b;
cout<<"Subtraction is:"<<sub<<endl;
}
inline void operation::multiplication() //using inline prefix
{
mul=a*b;
cout<<"Multiplication is:"<<mul<<endl;
}
inline void operation::division() //using inline prefix
{
div=(float)a/b; //data typecasting from int to float
cout<<"Division is:"<<div<<endl;
}
//Main function of program
int main()
{
cout<<"Program using inline function..\n";
operation o;
//calling member function
o.get();
o.addition();
o.subtraction();
o.multiplication();
o.division();
return 0;
}
5. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF FRIEND
FUNCTION IN C++.
//Demonstration of the working of FRIEND FUNCTION..

#include<iostream>
using namespace std;
class B; //declaration of class B
class A
{
private:
int a;
public:
//Member function
void getdata()
{
cout<<"Enter first no.:\n";
cin>>a;
}
friend float midvalue(A,B); //declaring friend function
};
class B
{
private:
int b;
public:
//Member function
void getdata()
{
cout<<"Enter second no.:\n";
cin>>b;
}
friend float midvalue(A,B); //friend function declaration
};
float midvalue(A a1,B b1) //friend function definition
{
return (float)(a1.a+b1.b)/2;
}
int main()
{
A a1;
B b1;
a1.getdata();
b1.getdata();
cout<<"Average of two number is:"<<midvalue(a1,b1)<<endl;
return 0;
}
6. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF CONSTRUCTOR
IN C++.

//Demonstration of the working of CONSTRUCTOR..

#include<iostream>
using namespace std;
class Myclass
{
public:
//Default constructor
Myclass()
{
cout<<"You are in the default constructor\n";
}
//Parametrized constructor having two arguments
Myclass(int a,int b)
{
cout<<"You are now in parametrized constructor which takes two arguments\n";
cout<<"Product of "<<a<<"&"<<b<<" is:"<<a*b<<endl;
}
};
//Main function of the program
int main()
{
Myclass obj; //the default constructor should have been invoked
Myclass obj1(3,4); //One way of creating object. Also known as implicit call to the constructor
Myclass obj2=Myclass(1,2); //Another way of creating object. This is known as explicit call to the
constructor.
return 0;
}
7. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF DESTRUCTOR IN
C++.
//Demonstration of the working of DESTRUCTOR..

#include<iostream>
using namespace std;
class Myclass
{
public:
//Constructor
Myclass()
{
cout<<"You are in constructor.\n";
}
//Destructor
~Myclass()
{
cout<<"You are in destructor.\n";
}
//Member function
void display()
{
cout<<"Hello!!\n";
}
};
//Main function
int main()
{
Myclass m; //object created
m.display(); //Member function called
return 0;
}
8. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF ARRAY OF
OBJECT IN C++.
//Demonstration of the working of ARRAY OF OBJECT..

#include<iostream>
using namespace std;
class Employee
{
private:
string name;
int salary;
public:
//Member functions
void getname()
{
//cin.ignore();
fflush(stdin);
getline(cin,name);
}
void getsalary()
{
cin>>salary;
}
void display()
{
cout<<name<<"\t";
cout<<salary<<endl;
}
};
//Main function of program
int main()
{
int i;
Employee e[5]; //Array of objects
for(i=0;i<5;i++)
{
cout<<"Employee "<<i+1<<endl;
cout<<"Enter name:\n";
e[i].getname(); //calling member function by array of object
cout<<"Enter salary:\n";
e[i].getsalary(); //calling member function by array of object
}
cout<<"\nDetails of employees:\n\n";
cout<<"Employee no."<<"\t\t"<<"Name"<<"\t"<<"Salary"<<endl;
for(i=0;i<5;i++)
{
cout<<i+1<<"\t\t\t";
e[i].display(); //calling member function by array of object
}
return 0;
}

9. WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF STATIC DATA


MEMBER AND FUNCTION IN C++.
//Demonstration of the working of STATIC DATA MEMBER AND FUNCTION..

#include <iostream>
using namespace std;
class test
{
int no;
static int c; //Static data member
public:
//Member function
void setno();
void dispno();
static void disp_c(); //Static data member function
};
void test::setno()
{
no=++c;
}
void test::dispno()
{
cout<<"Object number:"<<no<<endl;
}
//Definition of static data member function
void test::disp_c()
{
cout<<"Counter:"<<c<<endl;
}
int test::c; //Defining static data member
int main()
{
test t1,t2,t3; //objects of class test
//calling member function of class
t1.setno();
t2.setno();
test::disp_c(); //calling static data member function
t3.setno();
test::disp_c(); //calling static data member function
t1.dispno();
t2.dispno();
t3.dispno();
return 0;
}

10.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF SINGLE


INHERITANCE IN C++.
//Demonstration of the working of SINGLE INHERITANCE..

#include<iostream>
using namespace std;
class base //single base class
{
string a;
public:
//Member function
void getval_a(string);
string return_a();
};
class derived:public base //single derived class(inherit the base class)
{
string b,c;
public:
//Member function
void getval_b(string);
string concat();
void display();
};
//Declaration of member function
void base::getval_a(string X)
{
a=X;
}
string base::return_a()
{
return a;
}
void derived::getval_b(string X)
{
b=X;
}
string derived::concat()
{
c=return_a()+" "+b; //Nesting of member function because we cannot access the private member
of base class
return c;
}
void derived::display()
{
cout<<"Concatination of the strings is:\n"<<c;
}
int main()
{
string a,b;
derived obj; //Object of derived class
cout<<"Enter 1st string:\n";
getline(cin,a);
cout<<"Enter 2nd string:\n";
getline(cin,b);
obj.getval_a(a); //calling member function of base class by the object of derived class
obj.getval_b(b);
obj.concat();
obj.display();
return 0;
}
11.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF MULTIPLE
INHERITANCE IN C++.
//Demonstrate of the working of MULTIPLE INHERITANCE..

#include<iostream>
using namespace std;
class A //base class
{
protected:
int a;
public:
void get_a();
};
class B //base class
{
protected:
int b;
public:
void get_b();
};
class C:public A,public B //Derived class(Multiple inheritance)
{
float c;
public:
void avg();
void display();
};
//Definition of member functions
void A::get_a()
{
cout<<"Enter the value:\n";\
cin>>a;
}
void B::get_b()
{
cout<<"Enter the value:\n";\
cin>>b;
}
void C::avg()
{
c=(float)(a+b)/2;
}
void C::display()
{
cout<<"Average is:\n"<<c;
}
//Main function
int main()
{
C obj; //Object for derived class
//Calling member functions of all classes by the object of derived class
obj.get_a();
obj.get_b();
obj.avg();
obj.display();
return 0;
}

12.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF MULTILEVEL


INHERITANCE IN C++.
//Demonstration of the working of MULTILEVEL INHERITANCE..

#include<iostream>
using namespace std;
class student //base class
{
protected:
int roll;
string name;
public:
void getval();
void putval();
};
class marks:public student //derived class
{
protected:
int m1,m2;
public:
void getmarks();
void putmarks();
};
class result:public marks //derived class
{
private:
int tot;
public:
void display();
};
//Definition of member function
void student::getval()
{
cout<<"Enter roll no.:\n";
cin>>roll;
cout<<"Enter name:\n";
fflush(stdin);
getline(cin,name);
}
void student::putval()
{
cout<<"roll no.:"<<roll<<endl;
cout<<"Name:"<<name<<endl;
}
void marks::getmarks()
{
cout<<"Enter marks1:\n";
cin>>m1;
cout<<"Enter marks2:\n";
cin>>m2;
}
void marks::putmarks()
{
cout<<"Marks1:"<<m1<<endl;
cout<<"Marks2:"<<m2<<endl;
}
void result::display()
{
getval();
getmarks();
cout<<"\n\nDisplaying data..\n\n";
putval();
putmarks();
tot=m1+m2;
cout<<"Total is:"<<tot<<endl;
if((tot/2)>=80)
cout<<"Performed well.";
else
cout<<"Try to work hard";
}
int main()
{
result obj;
obj.display();
return 0;
}
13.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF HYBRID
INHERITANCE IN C++.
//Demonstration of the working of HYBRID INHERITANCE..

#include<iostream>
#include<math.h>
using namespace std;
class arithmatic //base class
{
protected:
int a,b;
public:
void getdata()
{
cout<<"For multiplication..\n";
cout<<"Enter 1st number:\n";
cin>>a;
cout<<"Enter 2nd number:\n";
cin>>b;
}
};
class multiply:public arithmatic //derived class
{
protected:
int mul;
public:
void mult()
{
mul=a*b;
}
};
class power //base class
{
protected:
int a,b,c;
public:
void powe()
{
cout<<"For power..\n";
cout<<"Enter base:\n";
cin>>a;
cout<<"Enter power:\n";
cin>>b;
c=pow(a,b);
}
};
class result:public multiply,public power //Hybrid inheritance
{
public:
void display()
{
cout<<"Displaying result.\n";
cout<<"Multiplication is:"<<mul<<endl;
cout<<"Power is:"<<c;
}
};
int main()
{
result r;
r.getdata();
r.powe();
r.mult();
r.display();
return 0;
}
14.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF HIERARCHICAL
INHERITANCE IN C++.
//Demonstration of the working of HIERARCHICAL INHERITANCE..

#include<iostream>
using namespace std;
class operations //base class
{
protected:
int a;
public:
void getval()
{
cout<<"Enter the value:\n";
cin>>a;
}
};
class square:public operations //derived class1
{
protected:
int b;
public:
void sq()
{
b=a*a;
cout<<"Square is:"<<b<<endl;
}
};
class factorial:public operations //derived class2
{
protected:
int c=1;
public:
void fact()
{
for(int j=a;j>=1;j--)
{
c=c*j;
}
cout<<"Factorial is:"<<c;
}
};
int main()
{
square sq; //object for derived class1
factorial ft; //object for derived class2
sq.getval();
sq.sq();
ft.getval();
ft.fact();
return 0;
}

15.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF VIRTUAL


FUNCTION IN C++.
//Demonstration of working of VIRTUAL FUNCTION..

#include<iostream>
using namespace std;
class base //base class
{
public:
virtual void out() //virtual function
{
cout<<"Base class out\n";
}
void print()
{
cout<<"Base class print\n";
}
};
class derived:public base //derived class
{
public:
void out()
{
cout<<"Derived class out\n";
}
void print()
{
cout<<"Derived class print\n";
}
};
int main()
{
base obj1;
derived obj2;
base *b; //base class pointer
cout<<"b points to base:\n\n";
b=&obj1; //base class pointer contains reference for base class
b->out();
b->print();
cout<<"\nb points derived:\n\n";
b=&obj2; //base class pointer contains reference for derived class
b->out();
b->print();
return 0;
}

16.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF PUBLIC,


PRIVATE AND PROTECTED VISIBILITY MODES IN C++.
//Demonstration of the PRIVATE,PROTECTED AND PUBLIC VISIBILITY MODE..

#include<iostream>
using namespace std;
class test
{
private:
int x;
protected:
int y;
public:
int z;
test() //constructor to initialize data members
{
x=1;
y=2;
z=3;
}
};
class derived1:private test
{
//y and z becomes private members of class derive and x remains private
public:
void showdata()
{
cout<<"Private access specifier:"<<endl;
cout << "x is not accessible" << endl;
cout << "value of y(become private in derived class) is " << y << endl;
cout << "value of z(become private in derived class) is " << z << endl;
}
};
class derived2:protected test
{
//y and z becomes protected members of class derive
public:
void showdata()
{
cout<<"Protected access specifier:"<<endl;
cout << "x is not accessible" << endl;
cout << "value of y(become protected in derived class) is " << y << endl;
cout << "value of z(become protected in derived class) is " << z << endl;
}
};
class derived3:public test
{
//y becomes protected and z becomes public members of class derive
public:
void showdata()
{
cout<<"Public access specifier:"<<endl;
cout << "x is not accessible" << endl;
cout << "value of y(remains protected in derived class) is " << y << endl;
cout << "value of z(remains public in derived class) is " << z << endl;
}
};
int main()
{
derived1 D1; //object of derived1 class
D1.showdata();
//D1.x = 1; not valid : private member can't be accessed outside of class
//D1.y = 2; not valid : y is now private member of derived class
//D1.z = 3; not valid : z is also now a private member of derived class

derived2 D2; //object of derived2 class


D2.showdata();
//D2.x = 1; not valid : private member can't be accessed outside of class
//D2.y = 2; not valid : y is now protected member of derived class
//D2.z = 3; not valid : z is also now a protected member of derived class

derived3 D3; //object of derived3 class


D3.showdata();
//D3.x = 1; not valid : private member can't be accessed outside of class
//D3.y = 2; not valid : y remains protected member of derived class
//D3.z = 3; valid : z also remains public member of derived class
return 0;
}

17.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF OVERLOADING


IN C++.
//Demonstration of working of OVERLOADING..

#include<iostream>
#include<math.h>
using namespace std;
class operation
{
public:
void cal(int a) //function overloading
{
cout<<"Square of "<<a<<" is:"<<a*a<<endl;
}
void cal(double X) //function overloading
{
cout<<"Absolute value of "<<X<<" is:";
if(X<0)
X=-X;
cout<<X<<endl;
}
void cal(int a,int b) //function overloading
{
int c;
c=pow(a,b);
cout<<"Power of base "<<a<<" to "<<b<<" is:"<<c<<endl;
}
void cal(string a,string b,string c) //function overloading
{
string d;
d=a+" "+b+" "+c;
cout<<"Concatination of the strings is:"<<d<<endl;
}
};
//main function
int main()
{
int a,c,d;
double b;
string p,q,r;
operation op; //object
cout<<"Enter any number to find the square:\n";
cin>>a;
op.cal(a);
cout<<"Enter any number to find the absolute value:\n";
cin>>b;
op.cal(b);
cout<<"Enter base and power:\n";
cin>>c>>d;
op.cal(c,d);
cout<<"Enter first name,middle name & last name:\n";
cin>>p>>q>>r;
op.cal(p,q,r);
return 0;
}
18.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF OVERRIDING IN
C++.
//Demonstration of the working of OVERRIDING..

#include <iostream>
using namespace std;
class animal
{
public:
void sound()
{
cout<<"Animals sound....:\n";
}
};
class lion:public animal
{
public:
void sound()
{
animal::sound(); //call overridden function
cout<<"lion roars.\n";
}
};
class dog:public animal
{
public:
void sound()
{
cout<<"Dog barks.\n";
}
};
int main()
{
lion A;
dog C;
A.sound(); //call overridden function from a member function of the derived class
C.sound();
return 0;
}
19.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF OPERATOR
OVERLOADING IN C++.
//Demonstration of the working of OPERATOR OVERLOADING..

#include<iostream>
using namespace std;

class TestClass {
private:
int real, over;
public:
// Constructor to initialize real and over to 0
TestClass(int rl = 0, int ov = 0) {
real = rl;
over = ov;
}
// Overload the + operator
TestClass operator + (TestClass const &obj) {
TestClass result;
result.real = real + obj.real;
result.over = over + obj.over;
return result;
}
void print() {
cout<<"Addition is:\n";
cout << real << " + i" << over << endl;
}
};
int main()
{
TestClass c1(2, 9), c2(1, 4);
// c1 calls the operator function
// c2 is passed as an argument to the function
TestClass c3 = c1 + c2;
c3.print();
}
20.WRITE A PROGRAM TO DEMONSTRATE THE WORKING OF FILES IN C++.
//Demonstration of the working of FILES..

#include<iostream>
#include<fstream>
using namespace std;
class files
{
fstream file;
public:
void open()
{
file.open("filec++.txt",ios::out); //opening/creating file in writing mode
if(!file)
{
cout<<"File creation failed.\n";
}
else
{
cout<<"New file created.\n";
file.close(); //closing file
}
}
void writing()
{
file.open("filec++.txt",ios::app); //opening file in append mode
if(!file)
{
cout<<"File creation failed.\n";
}
else
{
cout<<"Successfully written in the file.\n";
file<<"Starts learning file handling in c++."; //writing in the file
file.close();
}
}
void reading()
{
file.open("filec++.txt",ios::in); //opening file in read mode
if(!file)
{
cout<<"No such file exist..\n";
}
else
{
char c;
while(!file.eof())
{
file>>c; //reading from the file
cout<<c;
}
}
file.close();
}
void close()
{
file.close(); //closing file
cout<<"File closed.";
}
};
//Main function
int main()
{
char a;
files f; //object for files class
while(1){
cout<<"\nEnter your choice:\n";
cout<<"O.opening/creating file.\n";
cout<<"W.writing in the file.\n";
cout<<"R.reading from the file.\n";
cout<<"C.closing file.\n";
cout<<"E.exit.\n";
cin>>a;
switch(a)
{
case 'O':f.open();
break;
case 'W':f.writing();
break;
case 'R':f.reading();
break;
case 'C':f.close();
break;
case 'E':exit(0);
default:
cout<<"Invalid input.\n";
continue;
}
}
return 0;
}

You might also like