Help with class

Woke up this morning, went to work, decided "Free time I'll start coding a bit", so I started messing around with classes. A little background: No matter how much studying, I just can't get the hang of classes and functions. I haven't started making classes separate of the file, just inside the program outside the main function. So, my question is...

What am I doing wrong in declaring my class? I'll list the code and the error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <ctime> //Gonna do something with rand();
using namespace std;

Class Calc(){
    Public:
        double add(int x, int y){
            double answer = x + y;
            return answer;
        }
        double subt(int x, int y){
            double answer = x - y;
            return answer;
        }
        double mult(int x, int y){
            double answer = x * y;
            return answer;
        }
        double divide(int x, int y){
            double answer = x / y;
            return answer;
        }
};

int main(){

    Calc classObject;
    classObject.add(6, 7);
    return 0;

}


error I'm getting:
1
2
3
4
5
6
C:\Users\255484\Documents\CodeBlocks\Projects\Calculator\main.cpp|5|error: 'Class' does not name a type|
C:\Users\255484\Documents\CodeBlocks\Projects\Calculator\main.cpp||In function 'int main()':|
C:\Users\255484\Documents\CodeBlocks\Projects\Calculator\main.cpp|27|error: 'Calc' was not declared in this scope|
C:\Users\255484\Documents\CodeBlocks\Projects\Calculator\main.cpp|27|error: expected ';' before 'classObject'|
C:\Users\255484\Documents\CodeBlocks\Projects\Calculator\main.cpp|28|error: 'classObject' was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|

This is all really simple coding, I just don't wanna start doing 500+ lines of code as I started to in java, and stop because of a couple errors or frustration. Any help is appreciated.
Class Calc()

should be

Class Calc

Classes are not functions so they don't need the double parentheses at the end.
C++ is case sensitive. You must spell class and public with lower case letters. You should also not have parentheses after the class name on line 5.
class Calc (note the highlight)

However, ┬┐what's the point of that class? it looks more like a namespace.
Last edited on
ne555,

Really no point to it but to get me more familiar with working with classes. As I said, I've struggled for the longest time with them, and it's frustrating when you've written a ton and you get that error. Simple solution is what I'm seeing from the 1st 2 replies. I should be shot.
> but to get me more familiar with working with classes.
the thing is, that is not how you work with classes.

The idea is to send messages (methods) to objects.
The object will respond according to its state (members)

But in your case, all objects are equal,
and the having to create one to call its methods is just a nuisance.
Last edited on
If you have time, would you mind giving me an example? And by getting familiar, I just meant, the overall structure, and the declaration process
Topic archived. No new replies allowed.