+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Junior Member level 1
    Points: 263, Level: 3

    Join Date
    Dec 2017
    Posts
    16
    Helped
    0 / 0
    Points
    263
    Level
    3

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

    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

    •   AltAdvertisment

        
       

  2. #2
    Junior Member level 1
    Points: 84, Level: 1
    AskSensors's Avatar
    Join Date
    Oct 2018
    Location
    Paris
    Posts
    17
    Helped
    0 / 0
    Points
    84
    Level
    1

    Re: How to specify object name when using .h and .CPP 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?



    •   AltAdvertisment

        
       

  3. #3
    Junior Member level 1
    Points: 263, Level: 3

    Join Date
    Dec 2017
    Posts
    16
    Helped
    0 / 0
    Points
    263
    Level
    3

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

    Quote Originally Posted by AskSensors View Post
    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;
    }



    •   AltAdvertisment

        
       

  4. #4
    Junior Member level 1
    Points: 330, Level: 3

    Join Date
    Jul 2017
    Location
    Chicago
    Posts
    15
    Helped
    1 / 1
    Points
    330
    Level
    3

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

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



--[[ ]]--