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

C++ Assignment 1.: R.Kavibharath 1901102 Cse-B

The document contains 3 C++ programs that demonstrate polymorphism through inheritance and virtual functions. The first program defines base class Shape and derived classes Circle, Square, Triangle. It uses a Shape pointer to call the draw function on each object. The second program is similar but makes the base class draw function pure virtual. The third program defines base class Student and derived classes Engineering, Medicine, Science. It uses a Student pointer to get and display data for each object.

Uploaded by

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

C++ Assignment 1.: R.Kavibharath 1901102 Cse-B

The document contains 3 C++ programs that demonstrate polymorphism through inheritance and virtual functions. The first program defines base class Shape and derived classes Circle, Square, Triangle. It uses a Shape pointer to call the draw function on each object. The second program is similar but makes the base class draw function pure virtual. The third program defines base class Student and derived classes Engineering, Medicine, Science. It uses a Student pointer to get and display data for each object.

Uploaded by

Kavibharath R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C++ ASSIGNMENT

1.
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw()
{
cout<<"drawing"<<endl;
}
};
class circle : public Shape
{
public:
void draw()
{
cout<<"circle drawn"<<endl;
}
};
class square : public Shape
{
public:
void draw()
{

R.KAVIBHARATH 1901102 CSE-B


cout<<"square drawn"<<endl;
}
};
class triangle : public Shape
{
public:
void draw()
{
cout<<"triangle drawn"<<endl;
}
};
int main()
{
Shape *S;
circle c;
square s;
triangle t;
S=&c;
S->draw();
S=&s;
S->draw();
S=&t;
S->draw();
return 0;
}

R.KAVIBHARATH 1901102 CSE-B


Output:
Circle drawn
Square drawn
Triangle drawn

R.KAVIBHARATH 1901102 CSE-B


2. #include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw()=0;
};
class Circle : public Shape
{
void draw()
{
cout<<"Drawing Circle \n";
}
};
class Square : public Shape
{
void draw()
{
cout<<"Drawing Square \n";
}
};
class Triangle :public Shape
{
void draw()
{
cout<<"Drawing Triangle \n";
}

R.KAVIBHARATH 1901102 CSE-B


};
int main()
{
Shape *S;
circle c;
square s;
triangle t;
S=&c;
S->draw();
S=&s;
S->draw();
S=&t;
S->draw();
return 0;
}
Output:
Drawing circle
Drawing square
Drawing triangle

R.KAVIBHARATH 1901102 CSE-B


3. #include<iostream>
using namespace std;
class Student
{
protected:
char name[20], depart[20];
public:
virtual void get_data() = 0;
virtual void show_data() = 0;
};
class Engineering : public Student
{
public:
void get_data()
{
cout<<"Enter the information of Engineering student ";
cout<<"\nEnter the name of Student: ";
cin>>name;
cout<<"Enter Department: ";
cin>>depart;
}
void show_data()
{
cout<<"The Information of Engineering student is ";
cout<<"\nName : "<<name;
cout<<"\nDepartment: "<<depart;
}

R.KAVIBHARATH 1901102 CSE-B


};
class Medicine : public Student
{
public:
void get_data()
{
cout<<"Enter the information of Medicine student ";
cout<<"\nEnter Name: ";
cin>>name;
cout<<"Enter Department: ";
cin>>depart;
}
void show_data()
{
cout<<"\nThe information of Medicine student is ";
cout<<"\nName: "<<name;
cout<<"\n Department: "<<depart;
}
};
class Science : public Student
{
public:
void get_data()
{
cout<<"\nEnter information of Science Student: ";
cout<<"Enter name: ";
cin>>name;

R.KAVIBHARATH 1901102 CSE-B


cout<<"Enter Department: ";
cin>>depart;
}
void show_data()
{
cout<<"The informaion of Science student is ";
cout<<"\nName: "<<name;
cout<<"\nDepartment: "<<depart;
}
};
int main()
{
Student *stu[3];
stu[0] = new Engineering;
stu[1] = new Medicine;
stu[2] = new Science;
for(int i = 0; i<3; i++)
{
stu[i]->get_data();
}
for(int i = 0; i< 3; i++)
{
stu[i]->show_data();
}
return 0;
}

R.KAVIBHARATH 1901102 CSE-B


Output:
Enter the information of Engineering student:
Enter name: Kavi
Enter department: CSE
Enter the information of Medicine student:
Enter name: Ashwin
Enter department: Cardiology
Enter the information of Science student:
Enter name: Mike
Enter department: Physics

R.KAVIBHARATH 1901102 CSE-B

You might also like