C Mse1
C Mse1
Marks distribution
Continuous Evaluation
( Record +Conduction) 30
LAB MSE 20
Execution and writeup
(Part A + Part B) 10+10=20
PART A
PART B
1. Write a C++ program to create a class sample with integer ,character and
float data members. Demonstrate Constructor Overloading on this class
with all types of constructors including default argument constructor.
#include <iostream>
using namespace std;
class Sample
{
int i;
char c;
double d;
public:
Sample()
{
i=0;
c='\0';
d=0.0;
}
Sample(int x,char y,double z)
{
i=x;
c=y;
d=z;
}
Sample(Sample &s)
{
i=s.i;
c=s.c;
d=s.d;
}
Sample(int x,double z,char y='s')
{
i=x;
c=y;
d=z;
}
void display()
{
cout<<"i= "<<i<<endl;
cout<<"c= "<<c<<endl;
cout<<"f= "<<d<<endl;
}
};
int main()
{
Sample s1;
cout<<"S1: "<<endl;
s1.display();
Sample s2(10,'a',5.6);
cout<<"S2: "<<endl;
s2.display();
Sample s3(30,4.54);
cout<<"S3: "<<endl;
s3.display();
Sample s4(s2);
cout<<"S4: "<<endl;
s4.display();
return 0;
}
-------------------
2. Write a C++ program to demonstrate the working of dynamic
constructors using a class STRING with string as data member inside the
class, include appropriate member functions to display the object data
and a member function to concatenate two strings.
#include <iostream>
#include<string.h>
using namespace std;
class String
{
int length;
char *name;
public:
String(){}
String(char s[])
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void join(String A,String B)
{
length=A.length+B.length;
name=new char[length+1];
name=strcpy(name,A.name);
name=strcat(name,B.name);
}
void display()
{
cout<<"name= "<<name<<endl;
}
};
int main()
{
String s1("wel");
String s2("fare");
cout<<"s1: "<<endl;
s1.display();
cout<<"s2: "<<endl;
s2.display();
String s3;
s3.join(s1,s2);
cout<<"s3: "<<endl;
s3.display();
String s4("come");
cout<<"s4: "<<endl;
s4.display();
String s5;
s5.join(s1,s4);
cout<<"s5: "<<endl;
s5.display();
String s6("done");
cout<<"s6: "<<endl;
s6.display();
String s7;
s7.join(s1,s6);
cout<<"s7: "<<endl;
s7.display();
return 0;
}
--------------
3. Write a C++ program for the diagram using Hierarchical inheritance. Use
your own data members and member functions to display student details.
#include <iostream>
using namespace std;
class student
{
protected:
char name[30];
int age;
intusn;
public:
voidgetstudent()
{
cout<<"Enter name "<<endl;
cin>>name;
cout<<"Enter age "<<endl;
cin>>age;
cout<<"Enter usn "<<endl;
cin>>usn;
}
};
classmedical:public student
{
int year;
public:
voidgetmedical()
{
cout<<"Enter year"<<endl;
cin>>year;
}
void display()
{
cout<<"Medical student details: "<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"USN: "<<usn<<endl;
cout<<"Year: "<<year<<endl;
}
};
classengineering:public student
{
intsem;
char branch[30];
public:
voidgetengineering()
{
cout<<"Enter sem"<<endl;
cin>>sem;
cout<<"Enter branch"<<endl;
cin>>branch;
}
void display()
{
cout<<"Engineering student details: "<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"USN: "<<usn<<endl;
cout<<"Semester: "<<sem<<endl;
cout<<"Branch: "<<branch<<endl;
}
};
int main()
{
medical m1;
cout<<"Enter medical student details"<<endl;
m1.getstudent();
m1.getmedical();
m1.display();
engineering e1;
cout<<"Enter engineering student details"<<endl;
e1.getstudent();
e1.getengineering();
e1.display();
return 0;
}
-----------
#include <iostream>
using namespace std;
class Complex
{
int real;
float imag;
public:
void read(int r,float i)
{
real=r;
imag=i;
}
void display()
{
cout<<real<<"+i"<<imag<<endl;
}
Complex operator+(Complex c)
{
Complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
Complex operator-(Complex c)
{
Complex temp;
temp.real=real-c.real;
temp.imag=imag-c.imag;
return temp;
}
};
int main()
{
Complex c1,c2,c3,c4;
c1.read(1,1.4);
c2.read(3,2.2);
cout<<"C1: "<<endl;
c1.display();
cout<<"C2: "<<endl;
c2.display();
c3=c1+c2;
cout<<"C3:"<<endl;
c3.display();
c4=c2-c1;
cout<<"C4: "<<endl;
c4.display();
return 0;
}