0% found this document useful (0 votes)
94 views

Lab Programs

The document contains 10 questions related to C++ programming concepts. Each question provides a code snippet to demonstrate a concept such as classes, inheritance, polymorphism, operator overloading etc. The questions cover core OOPs concepts as well as advanced topics like virtual base classes and order of constructor execution in multi-level inheritance.

Uploaded by

Akshaan Angral
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Lab Programs

The document contains 10 questions related to C++ programming concepts. Each question provides a code snippet to demonstrate a concept such as classes, inheritance, polymorphism, operator overloading etc. The questions cover core OOPs concepts as well as advanced topics like virtual base classes and order of constructor execution in multi-level inheritance.

Uploaded by

Akshaan Angral
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lab programs:

Q1: Write an interactive program in C++ to display:


a) Factorial b) Sum of digits
b) Reverse of digits d) Fibonacci series up to n

#include<iostream>
using namespace std;
int main()
{
    int choice;
    cout<<"1. Factorial \n";
    cout<<"2. Sum of digits \n";
    cout<<"3. Reverse of digit \n";
    cout<<"4. Fibonacci series upto n \n";
    cout<<"Enter the choice (1,2,3,4) :  ";
    cin>>choice;
   

    switch (choice)

    {
    case 1:
        int n,fact;
        fact=1;
        cout<<"Enter the value of n: ";
        cin>>n;
       
        if ((n==0)||(n==1))
        fact=1;
        else
        {

         while(n>0)
           {
            fact=fact*(n);
            n--;
           }

        }
        cout<<fact;
     
        break;
    case 2:
        int num,sum,r;
        cout<<"Enter the number : ";
        cin>>num;
        sum=0;
        while(num>0)
        {
            r=num%10;
            sum=sum+r;
            num=num/10;

        }
        cout<<sum;
        break;
    case 3:
        int number,rem,ans;
        cout<<"Enter the number : ";
        cin>>number;
        ans=0;
        while(number>0)
        {
            rem=number%10;
            ans=rem+(ans*10);
            number=number/10;
        }
        cout<<ans;
        break;
    case 4:
        int p,f1=0,f2=1,f3=1;
        cout<<" Enter the value of n :";

        cin>>p;
        cout<<"Fibonnaci series : " <<f2;
        for (int i=1;i<p;i++)
            {f3=f2+f1;
            f1 = f2;
            f2=f3;

        cout<<"   "<<f3;}
        break;
    }
}

OUTPUT:

Q2: Write a program in C++ to create a class Student? Enter the


records and display the same? Test the program for all the access
specifiers and show their applicability.

#include<iostream>
using namespace std;

class student
{
int r;

protected:
    int m;
public:
    string name;
    void input(){

    cout<<"Enter name: ";


    cin>>name;
    cout<<"Enter enrollment no: ";
    cin>>r;
    cout<<"Enter total obtained: ";
    cin>>m;}
    int get_pvt(){
    return r; }
    string get_pub(){
    return name;}
    int get_pro(){
    return m;}
    };

int main()
{

int n;
cout<<"Enter the number of students : ";
cin>>n;
int i;
student a[100];
for(int i=0;i<n;i++)
 a[i].input();
for(int i=0;i<n;i++)
{
cout<<"name - "<<a[i].get_pub()<<endl;
cout<<"enrollment no. - "<<a[i].get_pvt()<<endl;
cout<<"total obtained - "<<a[i].get_pro()<<endl;
}

return 0;

OUTPUT:
Q3: In reference to the concept of objects, write a program in C++ to
count the number of individual objects and array objects created.

#include<iostream>
using namespace std;
int c=0;
int d=0;
class A
{
public:
A();
};
A::A()
{
c++;
}
class B
{
public:
B();
};
B::B()
{
d++;
}
int main()
{
A d1,d2,d4,d5;
B d3[40];
cout<<"Number of objects created:"<<c;
cout<<"\nNumber of array objects created:"<<d;
return 0;
}

OUTPUT:

Q4: Write a program in C++ to create an array of Objects, insert


records and display the record of a particular object?

#include<iostream>
using namespace std;
class one
{
protected:
int roll_no;
char name[50];
int n,i;
float marks;
public:
void get_data();
void show_data();
};
void one::get_data()
{
cout<<"enter roll No :-";
cin>>roll_no;
cout<<"\nenter name:-";
cin>>name;
cout<<"\nenter marks:-";
cin>>marks;
}
void one::show_data()
{
cout<<"\nRecords are: \n";
cout<<"\nRoll No: "<<roll_no;
cout<<"\nName is: "<<name;
cout<<"\nMarks are: "<<marks;
}
main()
{
int n,i;

one o[10];
cout<<"enter the number of students: ";
cin>>n;
for(i=1;i<=n;i++)
{
o[i].get_data();
}
cout<<"Enter the serial number : ";
cin>>i;
o[i].show_data();
}

OUTPUT:
Q5: Write a program in C++ to demonstrate the use of different types
of constructors in a program.

#include <iostream>
using namespace std;

class Student {
private:
    int roll;
    string name;
public:
   
    Student() {
        roll = 41;
        name = "Akshaan";
    }

   
    Student(int r, string n) {
        roll = r;
        name = n;
    }

   
    Student(const Student &s) {
        roll = s.roll;
        name = s.name;
    }

    void display() {
        cout << "Roll No: " << roll << endl;
        cout << "Name: " << name << endl;
    }
};

int main() {

    Student s1;
    cout << "Details of student using default constructor: " << endl;
    s1.display();

   
    Student s2(41, "Akshaan");
    cout << "\nDetails of student using parameterized constructor: " << endl;
    s2.display();

   
    Student s3(s2);
    cout << "\nDetails of student using copy constructor: " << endl;
    s3.display();

    return 0;
}

OUTPUT:

Q6: Write a program in C++ to explain the concept of function


overloading.
#include<iostream>
using namespace std;
class over
{
  int n1=10,n2=20;
    public:
    void func()
    {
        int add;
        add=n1+n2;
        cout<<"Addition "<<add<<endl;
    }
    void func(int x,int y)
    {
     int sub;
     sub=x-y;
    cout<<"Subtraction "<<sub<<endl;
    }
  void func(int a,int b,int c)
    {
        int multi;
        multi = a*b*c;
         cout<<"Multiplication "<<multi<<endl;
    }
    void func(double x,double y)
    {
     double div;
     div=x/y;
    cout<<"division "<<div<<endl;
    }

};

int main()
{
over obj;
obj.func();
obj.func(20,20);
obj.func(20,30,100);
obj.func(100.0,3.0);
return 0;

OUTPUT:
Q7: Write a program in C++ to perform all the arithmetic operations
on two complex numbers using operator overloading.

#include <iostream>
using namespace std;
class Complex {
 public:
 float real, imag;
 Complex operator+(Complex const &obj) {
 Complex res;
 res.real = real + obj.real;
 res.imag = imag + obj.imag;
 return res;
 }
 Complex operator-(Complex const &obj) {
 Complex res;
 res.real = real - obj.real;
 res.imag = imag - obj.imag;
 return res;
 }
 Complex operator*(Complex const &obj) {
 Complex res;
 res.real = real * obj.real - imag * obj.imag;
 res.imag = real * obj.imag + imag * obj.real;
 return res;
 }
 Complex operator/(Complex const &obj) {
 Complex res;
 float denominator = obj.real * obj.real + obj.imag * obj.imag;
 res.real = (real * obj.real + imag * obj.imag) / denominator;
 res.imag = (imag * obj.real - real * obj.imag) / denominator;
 return res;
 }
};
int main() {
 Complex c1, c2, result;
 cout << "Enter the real and imaginary parts of the first complex number: ";
 cin >> c1.real >> c1.imag;
 cout << "Enter the real and imaginary parts of the second complex number: ";
 cin >> c2.real >> c2.imag;
 result = c1 + c2;
 cout << "The sum is: " << result.real << " + " << result.imag << "i" << endl;
 result = c1 - c2;
 cout << "The difference is: " << result.real << " + " << result.imag << "i"
<< endl;
 result = c1 * c2;
 cout << "The product is: " << result.real << " + " << result.imag << "i" <<
endl;
 result = c1 / c2;
 cout << "The quotient is: " << result.real << " + " << result.imag << "i" <<
endl;
 return 0;
}

OUTPUT:

Q8: Write a program in C++ to illustrate the concept of multiple


inheritances.
#include <iostream>
using namespace std;

class Shape {
    protected:
        float width;
        float height;

    public:
        void setWidth(float w) {
            width = w;
        }
        void setHeight(float h) {
            height = h;
        }
};

class Color {
    public:
        string color;

    public:
        void setColor(string c) {
            color = c;
        }
};

class Rectangle: public Shape, public Color {


    public:
        float area() {
            return width * height;
        }
};

int main() {
    Rectangle rect;
    rect.setWidth(5);
    rect.setHeight(10);
    rect.setColor("blue");
    cout << "Area of the rectangle: " << rect.area() << endl;
    cout << "Color of the rectangle: " << rect.color << endl;
    return 0;
}
OUTPUT:

Q9: Write a program in C++ to demonstrate the concept of Virtual


Base Class.

#include <iostream>
using namespace std;
class A {
public:
  A() {
 cout << "A class constructor called" << endl;
 }
};
class B: virtual public A {
public:
 B() {
 cout << "B class constructor called." << endl;
 }
};
class C: virtual public A {
public:
 C() {
 cout << "C class constructor called." << endl;
 }
};
class D: public B, public C {
public:
 D() {
 cout << "D class constructor called." << endl;
 }
};
int main() {
 D D;
 return 0;
}

OUTPUT:

Q10: Write a program in C++ to illustrate the order of execution of


base class constructors in case of multi-level in heritance?

#include <iostream>
using namespace std;
class A {
public:
 A() {
 cout << "A constructor called" <<endl;
 }
};
class B : public A {
public:
 B() {
 cout << "B constructor called" << endl;
 }
};
class C : public B {
public:
 C() {
 cout << "C constructor called" << endl;
 }
};
int main() {
 C c;
 return 0;
}

OUTPUT:

You might also like