You are on page 1of 10

1]

#include <iostream>
#include <string>
using namespace std;
static int StudentId = 0;
static int ProfessorId = 0;
class Person
{
public:
string name;
int age;
virtual void getdata() {
cin >> name;
cin >> age;
}
virtual void putdata() {
cout << name << " " << age << " ";
}
};
class Student : public Person
{
public:
int marks[6];
int id;
Student() {
StudentId ++;
}
void getdata() {
Person::getdata();
for (int i = 0; i < 6; ++i) {
cin >> marks[i];
}
id = StudentId;
}

void putdata() {
Person::putdata();
cout << this->sum() << " " << id << endl;
}

int sum() {
int sum = 0;

for (int i = 0; i < 6; ++i) {


sum += marks[i];
}

return sum;
}
};

class Professor : public Person


{
public:
int publications;
int id;

Professor() {
ProfessorId ++;
}

void getdata() {
Person::getdata();
cin >> publications;
this->id = ProfessorId;
}

void putdata() {
Person::putdata();
cout << publications << " " << id << endl;
}
};
int main(){
int n, val;
cin>>n;
Person *per[n];

for(int i = 0;i < n;i++){


cin>>val;

if(val == 1){
per[i] = new Professor;
}
else per[i] = new Student;

per[i]->getdata();
}
for(int i=0;i<n;i++)
per[i]->putdata();
return 0;
}

2] Write C++ program to demonstrate the use of virtual function


#include<iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class\n";
}

void show()
{
cout << "show base class\n";
}
};

class derived : public base {


public:
void print()
{
cout << "print derived class\n";
}

void show()
{
cout << "show derived class\n";
}
};

int main()
{
base *bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
}
3] Write a c++ program to Add members of two different classes using friend functions
#include <iostream>
using namespace std;
class ClassB;
class ClassA {
public:
ClassA() : numA(12) {}

private:
int numA;
friend int add(ClassA, ClassB);
};

class ClassB {

public:
ClassB() : numB(1) {}
private:
int numB;
friend int add(ClassA, ClassB);
};
int add(ClassA objectA, ClassB objectB) {
return (objectA.numA + objectB.numB);
}
int main() {
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
return 0;
}

4] Write a c++ program to calculate the area of circle and square using pure virtual function
#include <iostream>
using namespace std;
class Shape {
protected:
float dimension;
public:
void getDimension() {
cin >> dimension;
}
virtual float calculateArea() = 0;
};
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension;
}
};
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
};
int main() {
Square square;
Circle circle;

cout << "Enter the length of the square: ";


square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;

cout << "\nEnter radius of the circle: ";


circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;

return 0;
}

5] Develop an abstract class polygon from which derive triangle and rectangle classes. Each
polygon
should contain the function area() to calculate the area of them. Invoke the function area()
to calculate
the area using pointer of base class.
#include <iostream>
using namespace std;
class Shape
{
protected:
double width, height;
public:
void set_data (double a, double b)
{
width = a;
height = b;
}
virtual double area()
{return 0;}
};

class Rectangle: public Shape


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

int main ()
{
Shape *sPtr;
Rectangle Rect;
sPtr = &Rect;
sPtr -> set_data (5,3);
cout << sPtr -> area() << endl;

return 0;
}

You might also like