C++ :: Can Object Of A Class Be Assigned To Another Of Different Class?
Nov 9, 2013
I fully understand that a variable of one simple data type cannot be assigned to another of a different data type in an assignment statement, except when type-conversion(i.e. casting) is employed. For structured data types (like classes), I know that the assignment operator can be overloaded; but this only addresses objects of the same type.
My problem is that I have read data from an input file into an object of one class, but I want to transfer the contents into an object of a different class in an assignment statement. Can this be realized given that the two operands of the assignment operator (=), in this case, would be of two different types?
View 5 Replies
ADVERTISEMENT
Jan 21, 2013
The case is like
class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}
in somefunction i want a pointer to current object of class A m new to c++
View 2 Replies
View Related
Dec 7, 2014
I'm trying to pass the value of an object created from a class file to a function outside of the "Int Main" function in the main.cpp file. I've successfully created the object, I just want to pass it to a void function but I'm getting the scope error below. I'm not sure how to correct. I'm not having much luck with research either (static variables?).
error: 'TicTacToe' was not declared in this scope
main.cpp
#include <iostream>
#include <cstdlib>
#include "Signature.h"
#include "Gameplay.h"
using namespace std;
// Initialize array
char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
[code]....
View 4 Replies
View Related
Apr 26, 2014
I have my main.cpp like this:
#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;
[Code]...
Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.
My base class header score.h looks like this:
#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;
[Code]...
You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....
The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....
How can I cause it to return the old values that are set in curve1.cpp?
Here is my curve2.cpp
#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}
Can I use a function to simply put values from old to new variables? If yes then how?
View 3 Replies
View Related
Aug 21, 2013
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....
View 4 Replies
View Related
Sep 20, 2013
Suppose I have two classes A and B so how to access object of class A in constructor of B's class as a parameter.
View 6 Replies
View Related
Feb 20, 2015
My thought is that I would need to establish a variable for the class in the header and call the class within the .cpp file
//class A.h
B b;
Then in class A, to create the object and use a method from the object I would -
//class A.cpp
A::A(){
b();
}
int someAmethod(){
b.bmethod();
}
View 6 Replies
View Related
Mar 7, 2014
I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock. Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.
class MySeqBuildBlockModule: public SeqBuildBlock {
friend class SeqBuildBlockIRns;
public:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In)// more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0)
[code]....
I would have like to intiantiate an object "myIRns_3" of a class defined in third party library
SeqBuildBlockIRns myIRns_3(pSBBList2);
and would like to access it from the prep function as:
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();
I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);
ERRORS encountered:
When I tried to do it inside the constructor, I get the following errors:
MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
When I tried to do it in private section, I get the following errors:
MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
View 5 Replies
View Related
Jul 14, 2014
Information:
I'm using Code::Blocks v12.11.
(I'm using C++/SDL2, but I think that's of no relevance)
Problem:
I create a class named "CSprite" in a "Sprite.hpp"-file. I create a "Sprite.cpp"-file, which includes the "Sprite.hpp"-file. I define the methods of the class "CSprite" in the "Sprite.cpp"-file.
When I try to create an object of "CSprite" in the class named "CPlayer" in the file "Player.hpp" I get an error message. (<-- Looks complicated I know, the code example will be more usefull than this)
Error in the build messages:
C:UsersLinoDocuments1 Data LinoFreizeit1 ProgrammierenC++ & SDL2The Running ManCPlayer.h|30|error: 'CSprite' does not name a type|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|
Code Example:
Sprite.hpp
#ifndef _SPRITE_HPP_
#define _SPRITE_HPP_
class CSprite {
[Code] .....
What did I miss? Did I include the wrong file? Or did I Forget to include the file? Why do I get the error message?
I also tried it with a pointer declaration and the "->" Operator but I got the same error message. I know I could just write a new function to load the texture in my "CPlayer"-class but this would not really answer my question.
View 15 Replies
View Related
Dec 13, 2012
#include "B.h"
class A {
public :
A()
{
s_b = new B();
b = new B();
[Code] ....
In my project i have seen static object as above . But not able to know what is the exact use of it and how they are different from general object .
View 2 Replies
View Related
Dec 21, 2013
I have two classes one called Date and the other is University , Date class has two overloaded operators , ostream and istream to take in the data and print them out :
ostream & operator<<(ostream & out, Date & x) {
out<< x.month << "/" << x.day << "/" << x.year ;
return out;
[Code] ....
The University class has an object of type Date called establishDate and I must use this to print out the date along with the University name and location, here's University class :
University.h
class University {
public:
University (); // constructor
friend ostream & operator<<(ostream & out, University & x); // print the university data
friend istream & operator>>(istream & in, University & x); // to read university data
[Code] .....
so how do I use the establishDate object ?
View 2 Replies
View Related
Jun 22, 2014
Sandy is of class Person, who converts to Muslim and takes on a new name Fatima, and has all the attributes of Sandy, with new Muslim attributes (e.g. religion == Islam, etc..). At this point, Sandy can be deleted, and Fatima, now of class Muslim will play Sandy's role henceforth. The problem is that due to her new address, all the people who knew Sandy does not know Fatima. Manually changing Sandy's address to Fatima's address for all those people who knew Sandy is clearly not an acceptable method. I looked through all the design patterns and cannot find one to solve this problem. how to improve the design? Here's my simplified code showing the problem:
#include <iostream>
#include <string>
#include <typeinfo>
class Person {
std::string name;
Person* bestFriend;
[Code]...
Output:
sandy = 0x32658, 6Person
Mary's best friend is Sandy.
fatima = 0x23fec0, 6Muslim
Mary's best friend is Sandy.
Of course, we want to have: Mary's best friend is Fatima,. with mary->getBestFriend()->getReligion() == Islam, etc... How to redesign the whole thing so that this is automated (assume there are thousands of people who know her)?
View 14 Replies
View Related
Jul 15, 2013
is it possible to don't declare an object for a class and use the function of this class ? i saw a code use this but i cannot find it again .
View 7 Replies
View Related
Jul 27, 2013
What is the purpose of having static object of the same class.
E.g.
class someObj{
public:
static someObj obj;
};
how the compiler treats this object
View 3 Replies
View Related
May 10, 2014
class abc {
public:
int i;
abc * foooo;
};
How do you call * foooo? Say I create:
abc a;
* foooo would have some values for int i.
To get int i of *foooo, I tried a.foooo.i, which doesn't work. How do you call it?
View 5 Replies
View Related
Jun 25, 2013
I am having trouble working with third party dll's, libs and header files. I am trying to call a function.here is the function that is suppose to be called.
bool COAuthSDK::GetRequestToken(CClientDetails &objClientDetails)
it has this info of what it needs :
Name IN/OUT Description
m_environment IN Optional. Possible values are SANDBOX (default) and LIVE.
m_strConsumerKey IN OAuth consumer key provided by E*TRADE
m_strConsumerSecret IN OAuth consumer secret provided by E*TRADE
m_strToken OUT Returned by the function if successful
m_strTokenSecret OUT Returned by the function if successful
m_strCallback IN Optional; default value is "oob"
here is the COAuthSDK header
#ifndef _OAUTHSDK_H_INCLUDED_
#define _OAUTHSDK_H_INCLUDED_
#include "ETCOMMONCommonDefs.h"
#include "ETCOMMONOAuthHelper.h"
using namespace std;
[code].....
when I try to build the function it says that its in the dll's so I know I have to call it.here is the link to the build site if needed URL....
View 1 Replies
View Related
Feb 5, 2014
hiclass Parent {
};
class Child : virtual public Parent {
};
What is the size of object of Class Child in following case?
View 17 Replies
View Related
Dec 24, 2014
I wanted to create a new class object and I want to name it from a string. Something like this:
string name = "Mike";
Customers name;
//to create a new object in customer class using the name string
Is there any way to do this?
View 11 Replies
View Related
Aug 4, 2013
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
struct bop {
string realname; //real name
[Code] ....
Okay, so first thing's first. The program will not compile due to lines 39-45. If I were to change those pointers into regular objects, it will not change the values of my class object. So what is the right way to do this?
I want the user to be able to input the # of employers/programmers into the system. But I cannot do that with an array of classes because when declaring an array; the array size must be constant.
View 6 Replies
View Related
Mar 17, 2014
I'm trying to pass a class object by reference.
total = mathfunction(i);
}
double mathfunction(retirement& j)
{
double R = 0.00, m = 0.00, r = 0.00, t = 0.00, totaled = 0.00,
numerator = 0.00, denom = 0.00, temp = 0.00;
[Code]....
I only tried to pass by reference because I figured passing by value would be a larger pain in the neck.
View 1 Replies
View Related
Nov 18, 2013
I have built LOG4CXX lib and DLL and trying to use it in my application
Loh.h
class Log
{
public:
[Code].....
In my main I am initializing Log class object and calling Debug method to log debug message to a file.
Issue I am facing is at if(CLogger::oLogger != NULL) which is always returning oLogger as NULL.
View 1 Replies
View Related
Oct 7, 2014
make a class that you can make only one Object of it.
For example if you have Class A.
Let's say you create one object A* a=new A();
Now the next time you, try to create another object. For example:
A* b=new A(); What will happen is that b will get the same object (or reference) as a. In other words hey'll be pointing towards the same place. So basically you'll only have one object of it created. I tried doing it but I couldn't quite make it.
Here is what I tried: (but couldn't complete the exercise)
class God
{
public:
static int num;
static God* god;
[Code]....
View 2 Replies
View Related
Mar 6, 2013
I want to make a class, named generation, having a vector<chromosome> type data member.
chromosome is also class name.
chromosome has a 'route' as data member, which type is class TArrayI defined in ROOT package.
the size of route is to be fixed according to parameters of generation class object.
class chromosome{
public: chromosome(){;}
....
protected:
TArrayI route;
};
class generation{
public: generation(int PopSize=300, int numCity = 36){
.........
}
vector<chromosome> Population;
};
I want each element, chromosome of Population, to have a numCity size of route.
View 2 Replies
View Related
May 1, 2013
I am trying to pass an object to an inherited clas by a constructor. I am having Cboard my board and i need to pass it to the object Cpawn.
Here under is my code:
main
Code:
#include<iostream>#include "Cboard.h"
#include "Cpawn.h"
#include "Cpiece.h"
void main(){
char location[50];
[Code] ....
View 3 Replies
View Related
Jan 23, 2013
I am trying to delete a speific element in an array of class objects. i am overwriting the element i waant to delete with the element after it. My algorithm works but the output is not correct, after debugging it seems my objects just dont copy, is there a way to copy a class object, i have looked up copy constructors and attempted to write one but it does not seem to have any effect on the output.
below is my code
class user {
string firstname, lastname, currentteam, position, status ;
int age ;
public:
user() {};
[Code] .....
View 4 Replies
View Related
Apr 27, 2014
I am new to C++. I am trying to create 5 instance object of class Test. I wonder if these two codes are the same.
Test testArray[5];
//which one is the correct way or what is the correct way?
for(i=0;i<5;i++)
{
Test testArray;
}
View 2 Replies
View Related