Advertisement
jyoung12387

Inheritance Example (Social Media Demo)

Feb 24th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SocialMedia
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Post post1 = new Post("Thanks for the Birthday Wishes", true, "Jason");
  10.             Console.WriteLine(post1.ToString());
  11.             ImagePost imagePost1 = new ImagePost("Check out my new shoes", "Jason",
  12.                 "https://images.com/newshoes.jpg", true);
  13.             Console.WriteLine(imagePost1);
  14.  
  15.             Console.ReadLine();
  16.         }
  17.     }
  18.  
  19.     class Post
  20.     {
  21.         private static int currentPostId;
  22.  
  23.         // properties
  24.         protected int ID { get; set; }
  25.         protected string Title { get; set; }
  26.         public string SendByUserName { get; set; }
  27.         public bool IsPublic { get; set; }  
  28.  
  29.         // Default constructor. If a derived class does not invoke a base class constructor
  30.         // explicitly, the default constructor is called implicitly.
  31.  
  32.         public Post()
  33.         {
  34.             ID = 0;
  35.             Title = "My First Post";
  36.             IsPublic = true;
  37.             SendByUserName = "Jason Young";
  38.         }
  39.  
  40.         // Instance constructor that has three parameters
  41.         public Post(string title, bool isPublic, string sendByUsername)
  42.         {
  43.             this.ID = GetNextID();
  44.             this.Title = title;
  45.             this.SendByUserName = sendByUsername;
  46.             this.IsPublic = isPublic;
  47.         }
  48.  
  49.         protected int GetNextID()
  50.         {
  51.             return ++currentPostId;
  52.         }
  53.  
  54.         public void Update(string title, bool isPublic)
  55.         {
  56.             this.Title = title;
  57.             this.IsPublic = isPublic;
  58.         }
  59.  
  60.         //Virtual method override of the ToString method that is inherited from System.Object
  61.         public override string ToString()
  62.         {
  63.             return String.Format("{0} - {1} - by {2}", this.ID, this.Title, this.SendByUserName);
  64.         }
  65.  
  66.     }
  67.  
  68.     //ImagePost derives form Post and adds a property (ImageURL) and two constructors
  69.     class ImagePost:Post
  70.     {
  71.         public string ImageURL { get; set; }
  72.  
  73.         public ImagePost() { } // Constructor
  74.      
  75.         public ImagePost(string title, string sendByUsername, string imageURL, bool isPublic)
  76.         {
  77.             this.ID = GetNextID();                  // these methods are inherited from the Post class
  78.             this.Title = title;
  79.             this.SendByUserName = sendByUsername;
  80.             this.IsPublic = isPublic;
  81.  
  82.             this.ImageURL = imageURL;               // this method is unique to ImagePost class
  83.         }
  84.  
  85.         //Virtual method override of the ToString method that is inherited from System.Object
  86.         public override string ToString()
  87.         {
  88.             return String.Format("{0} - {1} - {2} - by {3}", this.ID, this.Title, this.ImageURL, this.SendByUserName);
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement