I am a part time college student getting his butt kicked in C++.
I dont know how to access the members of the class in this header file. How do I do it? And please use plain english.
Thanks

// Specification file for the Complex class
// constructor included.
#ifndef Complex_H
#define Complex_H

class
Complex {

private:

const double _realPart;

const double _imaginaryPart;

public:

Complex(double realPart=1.1, double imaginaryPart=2.2): _realPart(realPart), _imaginaryPart(imaginaryPart)
{
    //The code in the right of the colon servers to initialize the constructor.
    // _realPart=realPart and _imaginaryPart=imaginaryPart.
}

void display() const;

double getRealPart() const { return _realPart; }

double getImaginaryPart() const { return _imaginaryPart; }

Complex multiply(const Complex& other) const;

Complex add(const Complex& other) const;

Complex subtract(const Complex& other) const;


};
#endif

Recommended Answers

All 2 Replies

say i had class foo in ex.h then i would call it like this

#include <ex.h>
#include <iostream>
using namespace std;
foo objectOne;
int main(){
//or if you wanted to call a function or variable without declaring an object
//just like namespaces
foo::color(RED);
cout<<foo::x;
objectOne.x=3 //example with using the object name instead of the //classes
}
//or if you wanted to call a function or variable without declaring an object
//just like namespaces
foo::color(RED);

cout<<foo::x;

This only works if the function or member variable is static so ignore that bit for a moment (see below for a link to more info). Otherwise you need an instance of the class.

objectOne.x=3 //example with using the object name instead of the 
//classes
}

That approach using the dot to access members is more along the lines of what you are looking for.
So in your main you could have

Complex c1(1,1);
Complex c2(5,13);
std::cout<<c1.getRealPart()<<std::endl;
std::cout<<c2.getImaginaryPart()<<std::endl;
//and then declare a third complex number c3, and use the add method of c1 to sum c1 and c2 and assign it to c3.

If you are interested in information on static member functions and variables give the bottom half of this a read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.