Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Quick Question

$
0
0
I'm testing a very simple polymorphic program, that uses strings and an int, but for some reason strings show as empty spaces only. Am I forgetting something or strings cannot be used in polymorphic base and concrete classes. Also, inheritance seems to be working partially, I hate to initialize fields, but the int " age " outputs as " 0 " instead of taking the value I set in the main driver which is "40" in this case. I'm using visual studio 2012, and I'm not sure if MS changed some syntax for this version of VS. Here is my code:

Thanks.

Abstract base class
#ifndef ABSTRACTOBJECT_H
#define ABSTRACTOBJECT_H
#include <iostream>
#include <string>

using namespace std;

class abstractObject{
protected:
	string firstName;
	string lastName;
	int age;
public:
	abstractObject(string firstNames="", string lastNames="", int ages=0){
	    firstName = firstNames;
		lastName = lastNames;
		age = ages;
	}
	virtual void setName(){
	      cout<< "The name is "<< firstName <<", and the age is "<< age <<endl;
	}
};
#endif



Concrete Class
#ifndef CONCRETECLASSONE_H
#define CONCRETECLASSONE_H
#include <iostream>
#include <string>
#include "abstractObject.h"


using namespace std;

class concreteClassOne: public abstractObject{
public:
	concreteClassOne(string firstNames=" ", string lastNames="", int ages=0){
	   abstractObject(firstName, lastName, age);  
	}
	void setName(){
		cout<< firstName <<" didn't throw the trash yesterday." << "He's too old, "
			<< age <<", and still irresponsible."<<endl;
	}
};
#endif




Main driver
#include <iostream>
#include <string>
#include "abstractObject.h"
#include "concreteClassOne.h"
#include "concreteClassTwo.h"

using namespace std;

int main(){

	abstractObject *concrete;
	concreteClassOne rec("Peter","McFarlan",40);
	concrete = &rec;
	concrete->setName();

	return 0;
}





Output:


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>