C/C++ :: Polymorphism Is Stopping Short Of Desired Class

Mar 2, 2014

I've been working on this project which inserts data for different types of books lately, and I'm trying to utilize inheritance and polymorphism. The issue I've been having is after I create an object with my MediaFactory, I go to insert data into that type of object, but it won't reach the correct child class. The parent class in this case is MediaData. The class I'm trying to reach is Childrens, and the class that falls in between is called Book. The function I'm calling to insert the information is setData(ifstream&). This function simply places the information from a txt file into an object with the insertion operator.

Right now the program runs into the setData function of Book instead of Childrens. I need Childrens so I can enter all the required attributes (title,author,year). The call to this function is in MediaManager through the function called buildMedia, and my test is running into the case if (type == 'Y'). From there a pointer of type MediaData is created and pointed to a new object returned of type Childrens. I'm using my debugger in Xcode which does show that the correct object type (Childrens) was created.

I've tried a couple things so far. First I created another function called getData. From there I passed in *media and infile (txt input file) into that function, which then passed it right into setData with a pointer to the object and infile in the parameter. That didn't work, so I also tried going back into Childrens and removing MediaData from all my virtual functions and replacing it with Book. I got the same result when that happened; It morphed into Book class instead.

I have a portion of my UML as a reference. Childrens is a Book; Book is a MediaData. I also included all code for MediaManager, MediaHash, MediaFactory, MediaData, Book, and Childrens.

I did not include the operator overloads in the .cpp files to eliminate some redundancy.

//-----------------------------------------------------------------------------
// MediaManager.h
// Manager class for MediaData type objects
//-----------------------------------------------------------------------------

#ifndef MediaManager_H
#define MediaManager_H
#include "MediaHash.h"
#include "MediaFactory.h"
#include "MediaData.h"
using namespace std;
class MediaManager {

[Code] ....

View 3 Replies


ADVERTISEMENT

C# :: Inheritance Or Base Class / Polymorphism?

Sep 16, 2014

I have couple of objects which are using some amount of methods. Right now my application is not OOP and i am going to transfer it to OOP. So i would create each class for my each object. Almost all methods are in use for each object, the only one thing which is changing some of those objects passing not all parameters inside specific method. I can go two ways one is prepare interface for all methods i got and each of my classes could implement its own definition for it but other way almost all would implement exactly the same methods but with different parameters so maybe its better to create base class then for each object do inheritance from base class (polymorphism). inside base class i can prepare base methods and classes which will inherit from that class would override those methods for requirements they want.

So imagine: You got:

Memory
CPU
Latency

and all of them using mostly all of those same methods (only arguments for one of them could be use different way):

ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()

Now its better to do like this e.g:

Base class: Stuff
prop: name, id, date ...
methods to ovveride: ExecuteQuery(), ExportToExcel() ...

classes: CPU, Memory, Latency (inheriting from Stuff)
ovveride methods and align its definition for specific class use (remember mostly only passing args are used or not by specific class)

or go with inheritance

Interface SomeMethods
ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()

and each class would implemet its own definition.

The most important thing is that those every objects mostly using all of those methods and what is diffrence that one object can use all of available parameters inside this method and other one no. What i should do? Go with interface or inheritance and polymporfizm inside base class?

View 2 Replies View Related

C++ :: Shape Class Polymorphism Compiler Error

Dec 10, 2014

Having issues with program to create a shape area calculator with circle square and rectangle. the uml goes as follows:

Where the UML has shape as the abstract class with public area():double, getName():string,and getDimensions:string, rectangle derived from shape with protected height, and width, and a public rectangle(h:double, w:double), followed by a derived square from rectangle with just a public square(h:double), and finally a circle derived from shape with a private radius, and a public circle(r:double).

[URL]

Have linked my program and it is giving me the following compiler errors:

error: 'qdebug' was not declared in this scope line 15 of main

error: cannot declare variable 'shp' to be of abstract type 'shape' line 22 of main

error: expected primary-expression before ')' token lines 29 -31 of main

(note previously had qstring as a header file yet changed to string since I was getting error qstring was not declared in this scope.)

View 5 Replies View Related

C++ :: Polymorphism On Class Instance Is Not Directly Applicable To Array

Mar 19, 2013

Below code produces run-time segmentation fault (core dumped) when execution reached line 53: delete [] array

Code 1:

#include <cstdlib>
#include <iostream>
using namespace std;
#define QUANTITY 5
class Parent {
protected:
int ID;

[Code] ....

Output of code 1:

Constructor of A: Instance created with ID=1804289383
Constructor of A: Instance created with ID=846930886
Constructor of A: Instance created with ID=1681692777
Constructor of A: Instance created with ID=1714636915
Constructor of A: Instance created with ID=1957747793
A::showID() -- ID is 1804289383
A::showID() -- ID is 846930886
A::showID() -- ID is 1681692777
A::showID() -- ID is 1714636915
A::showID() -- ID is 1957747793
Try to delete [] array..
Segmentation fault (core dumped)

Question: Why does segmentation fault happen in code 1 above?

View 9 Replies View Related

C++ ::  basic Polymorphism - Parent / Child Class Based Program

Oct 19, 2014

I am making a very basic parent/child class based program that shows polymorphism. It does not compile due to a few syntax errors reading "function call missing argument list. Lines 76 and 77, 81 and 82, and 86 and 87.

#include<iostream>
using namespace std;
class people {
public:
virtual void height(double h) = 0;
virtual void weight(double w) = 0;

[Code] ....

View 4 Replies View Related

C/C++ :: Menu Selection Does Not Continue To Desired Screen

Mar 8, 2014

Basically we have to create a system for loan amortization for vehicle company. The code is still in its development stage and I am having problems with one of the menu selections. all the other menu selections work fine except for menu selection 3. I'm quite stuck on it. When I run the program everything goes well except when I enter 3 at the main menu, it just exits the program instead of going to the screen for the menu selection.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main() {
char un, /*username*/
name, /*customer name*/

[Code] ....

View 1 Replies View Related

C :: Scanf With Stopping Condition

Mar 22, 2013

I wanted to input some numbers with scanf function, i can enter some numbers and if I input -1 to the scanf, the input must end. And the scanf function has limited input, the max that I can input is 40 numbers.example if enter 1 2 4 6 5 4 -1 the scanf function will ended and the result will be appear.I wanted to know how the scanf function is like that would be best for this problem, Code: scanf("%d", &n); the result if I input those number will be like

|
||
||||
||||||
|||||
||||

View 3 Replies View Related

C++ :: Stopping Threads Without Using Local Var?

Aug 31, 2014

Here's my code,

#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
void Blink();

[Code] .....

The problem is i don't want to use local vars , is ther anyway do end the loop safely(removing all the vars used in function and etc) without using a local var ?

View 3 Replies View Related

C++ :: Stopping Codes With A Key Press

Feb 25, 2013

I have a program that I made, which does a list of stuff, then checks if a key is pressed, and if it's pressed, stop the looping.

The problem is, it checks if the key is pressed once a loop, which means that if the list has delays, you have to hold the button down for a while (depending on the list).

Here's an example:
Move mouse;
Pause program for 2 seconds;
Move mouse;
Pause program for 2 seconds;
Check if key is pressed;

What ways can I fix this problem?

View 1 Replies View Related

C++ :: Stopping Infinite Loop?

Feb 20, 2013

The code below gives me the maximum odd number and the minimum even number of a list of numbers that the user type. However, for the loop to stop the user needs to type 1000. Is there a better way (without having to have a "stopping" number, just in case I need this number to be somewhere on the list) to get the same results?

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main() {
int x, maxi, mini;

[code]...

View 2 Replies View Related

C++ :: Why It Is Runtime Polymorphism

Apr 4, 2013

class Base
{
.....
.....
.....
virtual void display();

[code]....

in the above polymorphism why is it called runtime polymorphism when i can say seeing the code itself that display() function in derived gets executed with ptr->display(),so how does it become runtime polymorphism when i could get decide at compile itself ???

View 6 Replies View Related

C++ :: Difference Between Int And Short Int

Aug 19, 2014

size of int is 2 bytes and of short int is also 2 bytes.The range of values for int and short int are the same.

Then why int and short int are used? only int or short int is enough ....

View 4 Replies View Related

C/C++ :: How To Set Each Bit If The Variable Is Short

Jan 3, 2013

i am using the below logic. but it gives me error when i am setting the bit_position 15 or 16. i suspect, the problem with the integer range.

short bit_map, bit_position  
bit_map = bit_map | ( 1 << bit_position )

View 2 Replies View Related

C/C++ :: Read Numbers From Cin Then Sum Up And Stopping When 0 Entered

Jun 6, 2014

Write a program that reads numbers from cin and then sums them, stopping when 0 has been entered. Add the following to your program:

-A counter within the while loop that keeps track of the number of inputs entered.
-A comment that determines if the total value is less than 100 or greater than 100, as in the sample output
-Switch statements to determine the total number of inputs

Sample output:

Enter numbers, one per line. Enter 0 to end:
7
8
6
5
5
9
8
0

The total was 48.
The total number of inputs read is 8.
The total is less than 100.

The code that I've got so far lets me keep track of the inputs entered, and displays the total of the values, but I'm not sure how to do the part where it determines if the value is greater or less than 100.

#include <iostream>
using namespace std;
int main() {
int val, total=0;
int i=0;
cout << "Enter numbers, one per line. Enter 0 to end:
";

[Code] .....

View 2 Replies View Related

C/C++ :: Stopping Case For Asterisk Pattern?

Dec 1, 2014

So, I'm going to write a recursive function for a asterisk pattern but I'm stuck figuring out how to create a stopping case for it, better yet, I'm unable to describe the pattern completely.

*
**
-*
****
--*
--**
---*
********
-----*
-----**
------*
-----****
-------*
-------**
--------*

( - represent white spaces )

What I've been thinking:

* Every odd row has 1 * with 1 incremented white space

* Every "pair" of asterisks equals 8 total (EX. 8 one pair *'s, 4 two pair *'s, 2 four pair *'s)

Unfortunately, that's all I got. how I can represent this as I function. Once I figure out what my stopping case should be, I think I can do the coding on my own.

View 1 Replies View Related

C Sharp :: Stopping And Resuming Threads?

Jul 9, 2012

how do I stop and restart a Thread in C# once it reaches a certain location on a form then restart the thread once it goes outside of the location boundries?

I am trying to stop an animation thread (a bullet) once it reaches a block on a panel.

View 1 Replies View Related

C++ :: Calculate Tax Of Vehicle With Polymorphism?

Dec 29, 2012

How can we calculate tax of vehicle with polymorphism? There are 3 types of vehicles: long, commercial, and private vehicles' classes. There is a common class about all vehicles.

BASE class:
1.Brand and series name (such as Toyota Corrolla, Isuzu etc.)
2.Year of production (you can term it model)
3.Engine size in dm3
4.Owner’s name (including surname) and identity
5.Plate number.

COMMERCİAL :
1.Number of seats (apart from the driver)
2.Data to indicate whether the commercial vehicle is allowed to operate at night.

LONG :
1.The tonnage (max load in tons)
2.Data to indicate whether the long vehicle is allowed to carry goods internationally.

PRİVATE:
1.The class label which can be one of {A,B,C} .

View 1 Replies View Related

C++ :: How To Set Certain Bits Of Unsigned Short

Aug 8, 2013

I have a double variable and depending on certain conditions I need to set certain bits of an unsigned short Variable. For example, if double var is odd I need to set the 15th bit of the unsigned short variable.

View 4 Replies View Related

C# :: How To Use Encapsulation / Polymorphism And Inheritance In WPF

Oct 20, 2014

I have been reading up about object oriented programming recently and have come across 'Encapsulation, Polymorphism and Inheritance' as i understand it so far all OOP programs should use these three concepts. So i started thinking how do i get these concepts into my program as i am using WPF C# and i could not really find much good info about how these concepts apply to WPF programs.

Or do these concepts just not work with WPF programs?

View 5 Replies View Related

C++ :: Virtual Functions And Polymorphism

May 2, 2014

if we do a virtual functions(polymorphism) why we need re-declare the functions(when we create a new class derived from other)?

View 10 Replies View Related

C :: Shows Lines Of Running In Output With Gdb Without Stopping

Feb 24, 2013

I run the program with gdb , i searched but find nothing about how i could run gdb , that shows what line of code is running constantly (i rather it also shows value of the variable on each line if it's possible ) without stopping (i mean i don't want to enter "step" every time , i just need to run the program with debugger and shows line of the code is running (without need to enter step each time by myself)is it possible? if yes , what command is needed to start gdb for this purpose?

P.S: for this purpose if i have to set breakpint i will. but even i rather not set breakpoint , i rather gdb while running the program shows what line is now executing (rather with the value of variabels).

View 4 Replies View Related

C/C++ :: Stopping A Game When Winning Score Is Reached

Nov 11, 2014

I'm working on coding a game of pong that allows players to play until one reaches a score of 10. Once the winning score is reached I want to display a "Game Over" message stating which player won. The game works perfectly except for stopping when the winning score is reached.

Here is the code I have so far:

//pongTest.cpp
#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <math.h>
#include <glgl.h>
#include <glglu.h>
#include "GL/freeglut.h"

[Code] ....

Not sure if my gameOver() function just isn't written properly, I'm not calling it properly, or a mixture of both.

View 2 Replies View Related

C++ :: Does Polymorphism Affects Operators Overloading?

Nov 24, 2013

I must overload [] but at the same time I must use polymorphism. I wonder if using polymorphism affects operators overloading since when I modified the first class by writing "virtual":

virtual void mostrarDatos(char*, char*, char*);
virtual void calcularEdad(int);

So I can do the polymorphism, it affects the part of the code where suppose to do an addition:

s=student1+=student2;
t=student3+=student4;
u=s+=t;

if I do that, it shows some strange numbers instead of the right ones. Here is the complete code:

.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class persona {

[Code] ......

View 2 Replies View Related

C++ :: Returning Short Int Array In A Function

Feb 14, 2014

I'm trying to return a short int in a function..This is my function signature :

short int* StartRecord(int seconds)
this is my array :
short int waveIn[NUMPTS];

I'm trying to get the data into an array :

short int arr [] = StartRecord(3);

getting this error: Error2error C2440: 'initializing' : cannot convert from 'short *' to 'short []'

View 1 Replies View Related

C/C++ :: How To Null Terminate Unsigned Short

Jul 23, 2014

I've stored a binary pattern in what is interpreted as an unsigned short.

unsigned short byte_one = 128;

I've done some bitwise manipulation and want to store it back into an array, however, it needs to be null-terminated.

buf[1] = byte_one;

How do I null-terminate this?

View 3 Replies View Related

C++ :: Printf With Signed Short / Byte

Jul 17, 2012

I am using print/sprintf with a "%i" format string. Works fine if the input is indeed a 32bit integer. But how can i put in 8/16 bit (ie short & byte) 'integers'? If i just throw them in, they are always taken as unsigned, as the topmost bit/s is/are casted to zero... [URL] ....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved