C++ :: Month Class - Overloading Prefix And Postfix?

May 2, 2013

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.

View 5 Replies


ADVERTISEMENT

C :: Postfix And Prefix On Array Pointer

Apr 25, 2013

first code,

Code:

int aRRay[] = {2,4,8,-2,-1,100};
int *ptr;
int loop;
ptr = &aRRay[0];
}

[code].....

first code result, " aRRay[] " and " ptr + loop " are in the same result
second code result, " aRRay[] " and "ptr + loop " are in the same result too,
third code result, " aRRay[] " and "ptr + loop " are in the same result Except "aRRay[4] = -1 with ptr + 4 = 100 " and "aRRay[5] = 100 with ptr + 5 = 14"

Question:Why the third code had a different result ? Its only because of the increment ++ptr or the round brackets on the *(++ptr) ?

View 4 Replies View Related

Visual C++ :: Overriding Prefix / Postfix Operators

Oct 26, 2012

I have an assignment which includes overriding the prefix and postfix operators, and my teacher has provided what the output from the program should be. I've written the code and it's nearly perfect, except for one tiny error I can't seem to get right.

This is (most of) the code from the header--I left out a few of the parts that aren't relevant to my question:

Code:
using namespace std;
#include<string>
#include<iostream>
class NumDays {
private:
int hours;

[Code] ....

The two problem lines are supposed to be outputting 12 and 1.5, respectively, but are instead showing 13 and 1.625. I know that hours is being changed to 12 at the end of the overriden prefix operation in the line above them, so I don't understand why it returns to 13 again. What I need to change?

View 5 Replies View Related

C++ :: Operator Overloading Function (prefix ++)

Feb 4, 2014

I am writing the overloading operator function (prefix ++) according to my book.. but it doesnt work !!!!!!

But if i write:

void operator++() {
++x;
}

It works !!!!!

Here is my code according to the book that doesnt work as it should:

#include <iostream>
using namespace std;
class things {
int x,y,z;

[Code] ....

View 2 Replies View Related

C++ :: Postfix Operator Overloading Error

Jul 27, 2012

Recently, i successfully overloaded postfix operator to class counter by using Object and Class. Now, i want to overload same postfix operator to Inheritance. I created my code and generated in compiler. But, my compiler signaled me uncommon error(saw first time) and i couldn't generate any idea where my actually mistake is.

Here is my Code which objective is to count the number increasingly or decreasingly as per object created of CountDn class.

Code:
#include <iostream>
using namespace std;
class Counter // base class
{
protected : // NOTE : Not Private
unsigned int count;

[Code] ....

Error :|41|error: no 'operator++(int)' declared for postfix '++', trying prefix operator instead|
|42|error: no 'operator++(int)' declared for postfix '++', trying prefix operator instead|
|42|error: no match for 'operator=' in 'c2 = c1.CountDn::<anonymous>.Counter:perator++()'|
|44|error: no 'operator--(int)' declared for postfix '--', trying prefix operator instead|

View 10 Replies View Related

C++ :: Program That Outputs Calendar For That Month When Given Month And Year

Mar 17, 2013

i am beginner in c++ , i need to write a programm that out ouputs a calender for a month when given a month and year using this frame work :

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

[code].....

View 8 Replies View Related

C++ :: Converting Month Number To Month Name Using Exception Classes

Oct 29, 2013

program that prompts the user to enter a person’s date of birth in numeric form such as 8-27-1980.The program then outputs the date of birth in the form: august 27, 1980.Your program must contain at least two exception classes; invalidDay and invalidMonth.

View 1 Replies View Related

C++ :: Displaying Month Names From Class

Apr 10, 2013

I am having trouble associating the month names with the month numbers. When the program runs, it should output the date in three styles.

Also I do not know if my input validations work correctly(day cannot be greater than 31 or less than 1, month cannot be greater than 12 or less than 1).

#include <iostream>
using namespace std;
char again;
class Date {
private:
int month;
int day;
int year;

[Code] ....

View 7 Replies View Related

C++ :: Develop Month Calendar By Designing A Class - Inheritance And Composition

Nov 10, 2014

In this assignment the student should develop a month calendar by designing a class called calendarType . This class uses two other classes (dateType and dayType) as described below:

1. dayType Class: This class has been designed by students in Lab1 exercises. Referee to it.
2. dateType Class: This class is designed and implemented to keep track of data. This class has been provided it to you. Study it first then add definitions and implementations of the following operations to this class:

- Test whether the year is a leap year. Leap year is identified by 3 criteria :
- The year is evenly divisible by 4;
- If the year can be evenly divided by 100, it is NOT a leap year, unless;
- The year is also evenly divisible by 400. Then it is a leap year.
- Return the number of days in the month. For example, if the date is 12/3/2014, the number of days to be returned is 31 because there are 31 days in March.
- Return the number of days passed in the year. For example, if the date is 18/3/2014 the number of days passed is 77. Note that the number of days returned also includes the current day.
- Return the number of days remaining in the year. For example, if the date is
18/3/2014 , the number of days remaining in the year is 288.
- Calculate the new date by adding a fixed number of days to the date. For example, if the date is 18/3/2014 and the days to be added are 25, the new date is 12/4/2014.

To print monthly calendar using calendarType class, you must know the first day of the month and the number of the days in that month. Thus, you must store the first day of the month, which is in the form of dayType and the month and the year of the calendar. Clearly, the month and the year can be stored as an object of the form dateType by setting the day component of the date to 1, and the month and year as specified by user.

Design the class calendarType so that the program print a calendar for any month starting 1/1/ 1500 which is Monday. To calculate the first day of a month, you can add the 2 appropriate days to Monday of January 1, 1500. For this class identify and implement the following operations:

- Determine the first day of the month for which the calendar will be printed. Call this operation firstDayOfMonth.
- set/get month.
- set/get year.
- Print calendar for particular month.
- Add the appropriate constructors to initialize the member variables.
- Write a test program to print the calendar for either a particular year or a particular month.

View 1 Replies View Related

C++ :: Creating Infix To Postfix Program Through Inherited Class

Apr 25, 2014

I have the following problem on my C++ Program and I am not sure why it is not working. I am creating a infix to postfix program through an inherited class which I am not sure it is working.

#include <iostream>
#include <stack>
using namespace std;

int in_stack_Priority(char a){
if(a == '*' || a == '/')
return 2;

[Code] .....

View 3 Replies View Related

C++ :: Array Base Stack Class - Infix To Postfix Conversion

Feb 4, 2014

Array based stack class. So i am having a problem, i do not understand how to convert an infix notation into a postfix notation from a txt file. The infix expressions are stored in a text file. for example inside the text file is

6+9
2+7*8
(1+5)*3
5+(6-2)*((2-8)

at the moment my header file is

#ifndef STACKS_H
#define STACKS_H
#include <iostream>
using namespace std;
const int STACKS_CAPACITY = 128;
typedef int StacksElement;

[Code] ....

but how to make it convert the into postfix...

View 3 Replies View Related

C++ :: Function Overloading In Child Class?

Jan 17, 2014

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?

View 2 Replies View Related

C++ :: Operator Overloading In Inherited Class

Jun 2, 2013

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).

compiler is VS2008SP1.

View 11 Replies View Related

C++ :: Operator Overloading In Complex Number Class

May 29, 2013

Write c/c++ code of overloading ^operator in complex number class.

If we have two objects of complex number class as fellows,complex obj3 =obj1 ^obj2 ;

Obj3 reak and imaginary pars will be ,

Obj3.real = (obj1.real)obj2.real
And
obj3.img = (obj1.img)obj2.img

View 1 Replies View Related

C++ :: Arithmetic Operators Overloading For Class With Pointer

Nov 11, 2014

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.

View 3 Replies View Related

Visual C++ :: Class Fraction - Overloading Operators

Sep 25, 2013

the question am having problems with..

1.Write a class function that defines adding, subtracting, multiplying and dividing fractions by overloading standard operators for the operations.

2. Write a function member for reducing factors and overload I/O operators to input and output fractions. how would i set this up?

View 5 Replies View Related

C++ :: Creating A Class Called Time - Operator Overloading

Feb 18, 2015

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:

time operator- (const time& x, const time& y){
time subtract;
subtract.days = x.days - y.days;
subtract.hrs = x.hrs - y.hrs;
subtract.mins = x.mins - y.mins;
subtract.secs = x.secs - y.secs;

[Code] .....

View 1 Replies View Related

C++ :: Error Overloading Operator / Class Template Vector

Feb 7, 2013

I'm trying to implement a vector class in C + +. However when I go to actually run the main, it generates an interrupt operating system.

MAIN.cpp
#include "header.h"
int main() {
// Definisco le istanze delle classi
vettore <int> vet;
vettore <int> vet2;
vettore <int> vet3;

[code].....

View 7 Replies View Related

C/C++ :: Repeating Errors On Overloading Non Member Class Program

Oct 21, 2014

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:

#include "stdafx.h"
#include <iostream>
#include <cassert>

[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...

View 7 Replies View Related

C++ :: Overloading Binary And Assignment Operators In Vector Class

Feb 5, 2013

I am making a vector class and am having some problems creating the overloaded arithmetic operators and assignment operators.

Here is an example of my "+=" code as it stands the errors are similar/the same for the other operators except "=" operator which works fine:

Vector3& Vector3::operator+=(const Vector3 &rhs) {
Vector3 tmp = *this;
Set(tmp.getX() + rhs.getX(), tmp.getY() + rhs.getY(), tmp.getZ() + rhs.getZ());
return *this;
}

I have tried a lot of different approaches ad always get the error:

error: passing 'const Vector3' as 'this' argument of 'double Vector3::getX()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getY()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getZ()' discards qualifiers

View 5 Replies View Related

C++ :: Complex Class - Input / Output And Operator Overloading

Jun 30, 2013

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!

Code:
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex {
friend ostream & operator<<(ostream &, const Complex &);

[Code] .....

cout << c3 is Working fine But cout << c1 + c2 is not giving me correct output!

View 3 Replies View Related

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:

(4.86129e-270, -1.97785e-41)
(4.86143e-270, -1.97785e-41)

Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

[Code] ....

Also, I want to add a memberwise assignment function to assign the values of coord1 to coord2. How would I go about doing so?

View 7 Replies View Related

C :: Stack Conversion From Infix To Prefix

Sep 16, 2013

Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MAX 50

[Code] ....

[URL] .....

My output should look like this ..

Enter an expression in infix form: (9+6)/(2*4)^15

Specify output expression type,1) Prefix 2)Postfix
The Prefix expression is: /+96^*2415
----------------------------------------------
Enter an expression in infix form: (9+6)/(2*4)^15

Specify output expression type,1) Prefix 2)Postfix
The Postfix expression is:96+24*15^/

View 2 Replies View Related

C++ :: Inline Function Prefix - External Call And GCC?

Jan 17, 2014

I have some functions inlined using the inline function prefix. If the function is called from outside the file (so a seperate psp-gcc -O3 ... filename.c filename.o compile command, when only the function is changed), will the other files be updated too? (I'm using the pspsdk toolchain).

Example:

max.c
inline byte max(byte a, byte b) {
return a>b?a:b;
} use1.c
void use1() {
if (max(1,2)==0)

[Code] ....

If I compile this, next change the max function and recompile using make (the compiler only takes the changed max.c->max.o file, next links them together) will use1.c&use2.c be updated with the new max.c function?

View 6 Replies View Related

C :: Calculating First Tuesday Of Any Month In Given Year?

Jul 1, 2013

I'm trying to determine the first Tuesday of any month in a given year.. I'd prefer to use my own functions to arrive at a solution, but logically I could sorting these things out.

View 6 Replies View Related

C++ :: Printing Number Of Days In A Month

Dec 17, 2013

I write this program to print the number of days for the month when the year and the month enters. This is not giving any result and i think that problem is with calling functions.

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int leap_year(int year);
string days(string mounth);
string feb(string fab1);
void main(){
int year;

View 8 Replies View Related







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