Question

  • Creator
    Topic
  • #2220825

    Learning Object Oriented C

    Locked

    by the_parasite ·

    I’m trying to teach myself Object Oriented C++ (I know Funktion Oriented C and C++, and Object Oriented C# and Java) but I’m having a hard time grasping header files.
    I have written a small object oriented “hello world” program in C#, and I’m hoping someone here would translate it to the coresponding object oriented c++ code, with header files and such, as it would help me a lot, in understanding the concept.
    Thanks a lot in advance!
    – Stephen

    my code is:
    // Name.cs
    using System;
    namespace ConsoleApp
    {
    class Name
    {
    private String myName = “nothing”;
    public Name() {}
    public Name(String newName)
    {
    this.myName = newName;
    }
    public String getName()
    {
    return myName;
    }
    }
    }

    // Program.cs
    using System;
    namespace ConsoleApp
    {
    class Program
    {
    static void Main(string[] args)
    {
    Name myName = new Name(“Stephen”);
    Console.Out.WriteLine(“Hello “+myName.getName()+”!”);
    }
    }
    }

All Answers

  • Author
    Replies
    • #2831224

      Clarifications

      by the_parasite ·

      In reply to Learning Object Oriented C

      Clarifications

    • #2830378

      Figured it out

      by the_parasite ·

      In reply to Learning Object Oriented C

      I figured it out, so wanted to give my result, in case anyone else could use it.
      – Stephen
      my code:
      //Hello.h
      #include
      namespace ConsoleApp
      {
      namespace Hello
      {
      // begin class
      class Name
      {
      public:
      // empty constructor
      Name()
      {
      myName = “Nothing”;
      }
      // constructor
      Name(std::string _myName)
      {
      myName = _myName;
      }
      // destructor
      ~Name()
      {
      myName = “”;
      }
      // getter
      std::string getName()
      {
      return myName;
      }
      protected:
      private:
      std::string myName;
      // end class
      };
      }
      }

      // main.cpp
      #include “Hello.h”
      using namespace std;
      using namespace ConsoleApp::Hello;
      int main(int argc, char* argv[])
      {
      Name emptyName = Name::Name();
      Name myName = Name::Name(“Steffen”);
      cout<<"hello "<

Viewing 1 reply thread