Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Comparison Overloading

Comparison Overloading

Scheduled Pinned Locked Moved C#
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rig Maitra
    wrote on last edited by
    #1

    Hi, I have made a program to compare the pages of 2 books using the overloading concept. But i m not sure that is correct or not. Please see my code and tell me whether my program is logically correct or not based on the concept.. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace comp { class book1 { public int page1; } class book2:book1 { public int page2; public book2(int p1 ,int p2) { page1 = p1; page2 = p2; } public static bool operator <(book2 b1, book2 b2) // overloading { if (b1.page1 < b2.page2) return (true); else return (false); } public static bool operator >(book2 b1, book2 b2) { if (b1.page1 > b2.page2) return (true); else return (false); } } class Program { static void Main(string[] args) { book2 b2 = new book2(5,10); // passing parameters if (b1.page1 < b2.page2) { Console.WriteLine("book 1 has less pages than book 2"); } else if (b1.page1 > b2.page2) { Console.WriteLine("book1 has more pages than book 2"); } Console.ReadLine(); } } }

    R K 2 Replies Last reply
    0
    • R Rig Maitra

      Hi, I have made a program to compare the pages of 2 books using the overloading concept. But i m not sure that is correct or not. Please see my code and tell me whether my program is logically correct or not based on the concept.. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace comp { class book1 { public int page1; } class book2:book1 { public int page2; public book2(int p1 ,int p2) { page1 = p1; page2 = p2; } public static bool operator <(book2 b1, book2 b2) // overloading { if (b1.page1 < b2.page2) return (true); else return (false); } public static bool operator >(book2 b1, book2 b2) { if (b1.page1 > b2.page2) return (true); else return (false); } } class Program { static void Main(string[] args) { book2 b2 = new book2(5,10); // passing parameters if (b1.page1 < b2.page2) { Console.WriteLine("book 1 has less pages than book 2"); } else if (b1.page1 > b2.page2) { Console.WriteLine("book1 has more pages than book 2"); } Console.ReadLine(); } } }

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Whoa, there's quite a lot which is not right there. By the look you have overloaded operators to compare books, but you are actually comparing pages!

      Regards, Rob Philpott.

      R 1 Reply Last reply
      0
      • R Rob Philpott

        Whoa, there's quite a lot which is not right there. By the look you have overloaded operators to compare books, but you are actually comparing pages!

        Regards, Rob Philpott.

        R Offline
        R Offline
        Rig Maitra
        wrote on last edited by
        #3

        Hey rob, i thought better to compare the no. of pages of the books .. But if i compile this it wont print the message.( I m new to this overloading concept). What do you suggest how it should be then ??

        R OriginalGriffO 2 Replies Last reply
        0
        • R Rig Maitra

          Hey rob, i thought better to compare the no. of pages of the books .. But if i compile this it wont print the message.( I m new to this overloading concept). What do you suggest how it should be then ??

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          OK. Why do you have two book classes, one derived from the other? In OOD, when you derive something it specializes the thing it derives from. In this case, it adds a second integer member called Page2. What's going on here? If you can, avoid this as this is just a distraction from what you're trying to do - operator overloading.

          Regards, Rob Philpott.

          1 Reply Last reply
          0
          • R Rig Maitra

            Hey rob, i thought better to compare the no. of pages of the books .. But if i compile this it wont print the message.( I m new to this overloading concept). What do you suggest how it should be then ??

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            As Rob has said, deriving from a base class specialises something - a book does not derive from a previous book except when it is an expanded version: the Directors Cut if you like. Think about it: a "Book by Charlotte Bronte" derives from "Book", and so does a "Book by Terry Pratchett" - but that is pretty much the only thing they have in common: the elements which are common to all books (Pages, a cover, a title, an author) and so forth. You wouldn't overload a method to get a number of pages from two different books, because the number of pages is a property of the individual instance on the book. But... you might overload it to get different information which is also instance specific:

            public class Book
            {
            private int pages;
            private List<int> chapters = new List<int>();
            public int GetPages()
            {
            return pages;
            }
            public int GetPages(int chapter)
            {
            int lastPage = pages;
            if (chapter >= chapters.Count || chapter < 0) return 0;
            if (chapter != chapters.Count - 1) lastPage = chapters[chapter + 1];
            return lastPage - chapters[chapter];
            }
            }

            This has two overloaded GetPages methods. If you don't supply any parameters it returns the number of pages in the book. If you pass it an integer, it returns the pages in that chapter. The type of the parameter determines which version of the method gets called, and thus what the method does.

            Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • R Rig Maitra

              Hi, I have made a program to compare the pages of 2 books using the overloading concept. But i m not sure that is correct or not. Please see my code and tell me whether my program is logically correct or not based on the concept.. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace comp { class book1 { public int page1; } class book2:book1 { public int page2; public book2(int p1 ,int p2) { page1 = p1; page2 = p2; } public static bool operator <(book2 b1, book2 b2) // overloading { if (b1.page1 < b2.page2) return (true); else return (false); } public static bool operator >(book2 b1, book2 b2) { if (b1.page1 > b2.page2) return (true); else return (false); } } class Program { static void Main(string[] args) { book2 b2 = new book2(5,10); // passing parameters if (b1.page1 < b2.page2) { Console.WriteLine("book 1 has less pages than book 2"); } else if (b1.page1 > b2.page2) { Console.WriteLine("book1 has more pages than book 2"); } Console.ReadLine(); } } }

              K Offline
              K Offline
              KUMAR619
              wrote on last edited by
              #6

              You are right but you need make some modifications to simplify it.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups