Professional Documents
Culture Documents
Oodp Week8
Oodp Week8
#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;
return sum;
}
};
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];
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;
}
void show()
{
cout << "show base 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;
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;}
};
int main ()
{
Shape *sPtr;
Rectangle Rect;
sPtr = &Rect;
sPtr -> set_data (5,3);
cout << sPtr -> area() << endl;
return 0;
}