C++ :: Extractor Overloading And Member Wise Assignment For Cartesian Class
Apr 6, 2014
I am making a program with a Cartesian class. I want the user to be able to input 2 coordinates, but when I run it it doesn't ask for any values to be entered. It gives this output Please enter the first coordinates: Please enter the second coordinates:
I am having a issues with an assignment in my class and don't really understand why. I am getting undeclared identifier errors even though I have declared and I am also getting an error. Here is the code:
Last time I came to you all with an error it was a simple brain fart on my part but I don't think this one is like that. I would love to tell you what the program is supposed to do but I still do not really know, which might be part of the problem. I guess it outputs different sized rectangles...
I've written the code for the row-wise sum of a matrix:
int rowsum=0; //row-wise sum cout<<"Row-wise sum:"<<endl; for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { rowsum+=arr[i][j]; } cout<<rowsum<<endl; rowsum=0; }
It works fine but I can't figure out how to do the column-wise sum.
I am trying to make a program with a Cartesian class that allows the user to enter 2 coordinates and displays these coordinates. When I try to compile it, I get a message saying the x and y in x=c and y=d don't name a type. How can I fix this? Also, how would I go about inserting an assignment function that assigns the values of coord1 to coord2?
Code: #include <iostream> #include <istream> #include <ostream> using namespace std; class Cartesian { private: double x; double y;
The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':
#include <iostream> #include <string> struct Person { std::string name; int ID, age, height, weight;
[Code] .....
Name = Bob ID = 2047 Age = 38 Height = 183 Weight = 170
Name = Frank ID = 5025 Age = 25 Height = 190 Weight = 205
Bob pretends to be Frank, but keeps his name and ID.
Name = Bob ID = 2047 Age = 25 Height = 190 Weight = 205
But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.
I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.
Here is the class definition:
template <class dataType> class myArray { public: void setArrayData();
[code]....
And here is the definition of the overloaded assignment operator:
And here is my main function that tests the operations on objects of the class:
int main(){ //object declarations myArray<double> list(5); //a single-parameter object declaration of class myArray myArray<double> myList(2,13); //a two-parameter object declaration of class myArray
[code]....
The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!
I am working Prata's C++ Primer 6ed. The exercise sequence of Listing 6.9 through Listing 6.11 involved using a switch statement with integer input (switch.cpp: 6.9), character input (switchchar.cpp: 6.10), and using integer input with enum (enum.cpp: 6.11). The main function of each was similar, especially in the usage of the input variable used by cin ("code" in the code provided below).
With switch.cpp, it was shown that entering a character caused the loop to cycle continuously. This was because the "cin >> choice" call failed to read the character into the int (choice) variable. The 'cin.fail() test followed by cin.clear().get() was added to switch.cpp to prevent the bad read from producing the loop spin.
However, in the original version of enum.cpp (below), which also uses integer input, if I input an 'm', the response is:
Enter color code (0-6): m Bye!Press any key to continue . . .
In other words, with character input the int based read operation apparently did not fail. The main methods were very similar, but I noticed that switch.cpp used the "!=" operator in the while loop's test expression. So I tried it in enum.cpp and it is commented out in the code below. I found that with that code in place the inability to handle character input was present. When a character is input the failing response is:
Enter color code (0-6): Always include default case. Enter color code (0-6): Always include default case. Enter color code (0-6): Always include default case. ... ...
I assume this is a VS2K12 quirk, but I will likely try something like gcc at work tomorrow. I think it would be easy to chase this though the iostream code, for someone who has already been there. But it is going on 1AM, and I am not certain it would be time well spent (i.e. how often do you "really" need to debug iostream code?). I did use the debugger and see that (I believe) _OK is set to false.
// enum.cpp -- Listing 6.11 (PG 279): Using enum (091513) #include <iostream> // const int red = 0, orange = 1, .... enum {red, orange, yellow, green, blue, violet, indigo}; int main() { using namespace std; int code;
i am trying to create the assignment operator for a class that uses a pointer for it's private variable. The error is saying expected constructor, deconstructor, or type conversion before "operator. (which is the assignment operator. I have tried everything i could think of or find online and nothing has worked. below is the code for the assignment operator in the .h file and the .cpp file.
I am facing some problems while overloading base class functoin in child class. I have 2 programs as listed below.
Program 1 :
#include <iostream> using namespace std; class base {
[Code].....
Compilation Errors:
child_overload.cpp: In function "int main()": child_overload.cpp:27: error: no matching function for call to "child::func(const char [16])" child_overload.cpp:17: note: candidates are: void child::func(double)
I thought as base class members are also as part of child class through "public" access specifier, it should access base class function, when funct() is called with a string. if I use "using base::func" in child, it works fine. But why I need that when base class memebers are part of child class?
So i am having troubles with operator overloading in inherited class. Basically, it doesnt work. Consider this:
Code:
class A { public: A() { x=0; z= new int;
[Code] ....
Some how the copy constructor of a is improperly executed - the pointer is copied over, not re-created. As a result, the destructors crashes due to double-free.
*/ B bb = b; //doesnt work B bbb(b); //doesnt work B bbbb(b, 0); //works }
Above code shows the problem well. The "official" copy-constructor wont work - it copies over the pointer directly, and doesnt create a new one as it should. However, if i provide my own pseudo-copy-constructor that works. But ofcourse it's just a cheap work around - and wont actually work in real code (STL).
I am stucked in a problem of overloading arithmetic operators such as "+,*" for a class in the form
class Point { int N; // dimension of the point double *Pos; // length of N }
My assign operator is : Point& Point::operator= (const Point& pt) { N= pt.N; if(Pos == NULL) Pos = new double[N]; memcpy(Pos, pt.Pos, N*sizeof(double));
[Code] ....
The add operator "+" is: Point operator+( const Point& pt1, const Point& pt2 ) { Point ptr = Point(pt); // this is a constructor for (int i=0; i<pt1.N; i++) ptr.Pos[i] += pt2.Pos[i]; return ptr; }
Based on the above overloading, What I am going to do is :
P = alpha*P1 + beta*P2; // alpha and beta are double constants, P1 and P2 are Points objes
It is ok with Intel C++ 14.0 compiler, but does not work with the microsoft visual c++ 2012 compiler in debug mode in visual studio 2012.
I stepped in those operators and found that visual c++ compiler deconstructs the ptr in operators "*" and "+" before its return while intel c++ finished the operation P = alpha*P1 + beta*P2; and delete those ptrs at last.
Portability of my operator overloading is worse. How to get those arithmetic operators overloading for class with pointers in it.
The objective is to build a month class that hold data on the number of the month and the name of the month. Using constructors and overloads, set it up to where you can input either the month or the name and it will output the results for both the month number and name.
Here is the code I have so far:
#include<iostream> #include<string> using namespace std; class Month{ private: string name; int monthNumber;
[Code] ....
It is almost fully compiled if I go by the error list. The only problems I see to be having are with the prefix and postfix overloads.
I have 2 points in a cartesian coordinate system, where the first point is always (0,0). Given the second point and a heading in degrees, how would I write an algorithm to determine if the 2nd point is traveling in the general direction of (0,0) ?
For example:
Point a (0,0) Point b (10,0) Point b heading = 270 degrees
This would be true this 270 degrees points right and point b is 10 units right of point a. I would probably also want this is be fuzzy somewhat, in that anything from say + or - 30 degrees would also be true, or something like this.
I am creating a class called time and we've had to do operator overloading for <, > , <=, >=, ==, !=, ++, --, >>, <<, * , +, and -.
Well I have done and error checked them all. The only one I cannot seem to get right is the minus and its because of the error checking. I am having issues with times like this
t1 = 0:0:2:3 t2 = 0:0:1:4
t1 - t2 should equal 0:0:0:59 but it returns 0:0:1:-1. (days:hours:minutes:seconds)
I need it to check for all cases and I just do not know how. Here is the code I have so far:
I wrote a simple Complex Class and overload input/output and +/- Operators in it!But there is something that doesn't work correctly!I can print an object of that class but I can't print sum of two object or something like that!
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.
Employee 1/7-1/13 1/14-1/20 1/21-1/27 1/28-2/3 and so on... A sum of values for this 7 days B C D
I want to add all the values of Job1 for each employee in a time range which is one week starting from monday to sunday. How can I group the Job1 values week wise and store them in another datatable.