How to specify object name when using .h and .CPP file

Status
Not open for further replies.

NewbeeAVR

Junior Member level 2
Joined
Dec 2, 2017
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
195
I have LCD class and a object lcd as follows.

Code:
class LCD{
public:
...
...
}lcd;

How to specify lcd object with two files lcd.h and lcd.cpp
 

Hello,
I don't see what you mean.
Could you specify :
- The IDE you are using?
- Where are you putting the main code ? is in a main file?
 

Hello,
I don't see what you mean.
Could you specify :
- The IDE you are using?
- Where are you putting the main code ? is in a main file?

I am using DevC++.
I need to write
lcd.h
Code:
class LCD{
void display(char);
};

lcd.cpp
Code:
void LCD :: display(char c){
//my code
}

Then in main.cpp
Code:
#include <iostream>
using namespace std;

int main(){
LCD lcd; // Is any way to not specify hear but anywhere else in lcd.h or lcd.cpp
lcd.display(A);
return 0;
}
 

You are creating an instance of the object in your main.cpp. It will work you just need to include the header file in your main.cpp - #include "lcd.h"

Here's how I use my LCD class:

LCD.h:
void SendCharacter(unsigned char character);

LCD.cpp:
void LCDInfo::SendCharacter(unsigned char character)
{
//My Code here
}

then in main.cpp:
LCDInfo *objLCD;
objLCD = new LCDInfo(); //I use a pointer

//Call some functions:
objLCD->InitLCD();
objLCD->SendCharacter(chCurrDisplayCharacter);

etc....
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…