Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » C++ » Solved Programs on C++ » C++ program on inheritance and virtual functions

C++ program on inheritance and virtual functions

#include<string.h>
#include<iostream>

using namespace std;

class Ship{
public: 
char name[50];
char yearBuilt[10];

Ship(){
   //nothing to do
}
    
Ship(char *name, char *yearBuilt){
strcpy(this->name, name);
strcpy(this->yearBuilt, yearBuilt);
}

virtual void print(){
cout<<" Name: "<name, name);
    strcpy(this->yearBuilt, yearBuilt);
	this->passenger = passenger;
   }
   
void print(){
   cout<<" Name: "<name, name);
	strcpy(this->yearBuilt, yearBuilt);
	this->capacity = capacity;
}
	   
void print(){
	cout<<" Name: "<print();
    cout << "----------------------------\n";
}

return 0;
}

Output of program (compiled on g++ compiler)

C++ example of inheritance and virtual function