User Tools

Site Tools


Sidebar

projects

wcp1 (due 20240124)
pct0 (bonus; due 20240125)
pct1 (bonus; due 20240125)
abc0 (due 20240131)
pct2 (due 20240131)
wcp2 (due 20240131)
gtf0 (due 20240207)
pct3 (bonus; due 20240207)
wcp3 (due 20240207)
dtr0 (due 20240214)
pct4 (due 20240214)
wcp4 (due 20240214)
bwp1 (bonus; due 20240228)
mmf0 (due 20240228)
pct5 (bonus; due 20240228)
wcp5 (due 20240228)
cnv0 (due 20240306)
gfo0 (due 20240306)
pct6 (due 20240306)
wcp6 (due 20240306)
cnv1 (due 20240313)
fwg0 (due 20240313)
pct7 (bonus; due 20240313)
wcp7 (due 20240313)
fwg1 (due 20240320)
pct8 (due 20240320)
wcp8 (due 20240320)
pct9 (bonus; due 20240327)
wcp9 (due 20240327)
bwp2 (bonus; due 20240410)
fwg2 (due 20240410)
fwg3 (due 20240410)
gfo1 (due 20240410)
pctA (due 20240410)
wcpA (due 20240410)
cpp0 (due 20240417)
pctB (bonus; due 20240417)
wcpB (due 20240417)
cpp1 (due 20240424)
pctC (due 20240424)
wcpC (due 20240424)
pctD (bonus; due 20240501)
wcpD (bonus; due 20240501)
gfo2 (due 20240508)
pctE (bonus; due 20240508)
wcpE (bonus; due 20240508)
EoCE (due 20240516)
haas:spring2024:cprog:projects:oop1

Corning Community College

CSCS1320 C/C++ Programming

PROJECT: Object-Oriented Programming (OOP1)

Objective

Explore and create a C++ program that utilizes inheritance.

C++

Inheritance

Classes that inherit from another class acquire the attributes and behaviors of the class they inherit from.

Classes that inherit from another are called derived classes, child classes, or subclass. Classes that are inherited from are called base classes, parent classes, or superclasses.

To inherit traits from another class use a colon after the class's declaration, followed by the name of the base class (and optionally an access specifier).

class Derived : public Base

A class can inherit data or functions from multiple base classes. In order to do this, we would create a class derivation list, which names the base classes. The form would be the same as above, however, each class would be separated with a colon.

Problems can be created when a derived class inherits from more than one base class. An example of this is when two or more base classes have a member with the same name. The problem here is the derived class cannot distinguish between the two members.

If you have a function expecting an argument of a base class's type, you can pass objects of a type that derived from said base class.

For example:

#include <cstdio>
 
class Vehicle
{
public:
	float gas{ 15.53f };
};
 
class Car : public Vehicle
{
public:
	unsigned short tires{ 4 };
};
 
void print_gas(const Vehicle& vehicle)
{
	std::printf("The vehicle has %.2f gallons of gas left\n", vehicle.gas);
}

You are able to pass an object of type Car to print_gas.

Car myCar{};
print_gas(myCar);

Access Control implications

Inheriting from a base class using the public access specifier means public members of the base class will become public members of the derived class, and protected members of the base class will become protected members of the derived class. Private members of the base class will still be private.

A class with the keyword struct, and unions, will have public access by default, for their members and base classes.

Inheriting from a base class using the protected access specifier means both public and protected members of the base class will become protected members of the derived class. Private members of the base class will still be private.

Inheriting from a base class using the private access specifier means all members of the base class will become private members of the derived class.

A class with the keyword class will have private access for its members and base classes by default.

Parent-Child Relationships

The Child class is being referred to the class that inherits from another class, and the Parent class is being referred to the class that is being inherited from.

Since the Child simply inherits the properties of the Parent, one Parent can have multiple Children. Two Children of the same Parent will also have no direct relationship. Children can themselves have Children that inherit from them, creating large inheritance chains. Children can also multi-inherit, taking on the attributes of multiple Parents.

Parent/Child inheritance chains are a beneficial way to add specifications to classes without changing the base class functions. An example of this may be Animals→Dogs: An animal would have a size, age, living/death status, which are all properties that a dog could inherit, however, a dog could have it's own functions like fur length, temperament, so on and so forth. This could be extended by creating a dog breed class.

It’s also possible to inherit from a class that is itself derived from another class

A reflection on the concept of inheritance

(among other things)

It is easier to understand inheritance than it is most concepts in programming, because inheritance follows so closely to where it gets its name from. If you asked a non-computer programmer what a string is it would likely make them think of a string of fabric, whereas if you asked them what inheritance is they would probably think of a concept the same as how inheritance functions in C++.

Something Interesting to note is that child class cannot access the private information relating to their parents, but can still have them. For example, if plant(as a parent class of tree) has a private int of age, then tree will also have a private value of age. This is because when inheritance is activated it calls upon the parent's constructor, which can access the parent's private values.

When something has multiple parents it is called multiple inheritance. When there is more than one tier of inheritance (grandparents) it is called multilevel inheritance. When something has multiple children it is called hierarchical inheritance. When multiple of these occur it is called hybrid inheritance.

Inheritance allows us to reuse classes by having other classes inherit their members.

It is possible to make a class non-subclassable (sterile) in C++ with the “final” keyword.

COOL BUG FACT: Inheritance was first created for the Simula programming languages

Program

Write a program that creates a parent-child class structure. It can be anything, so long as you genuinely implement it and it works. A few possible examples for inspiration:

  • shape → rectangle
  • vehicle → car
  • fruit → strawberry

Be sure to make use of and follow proper conventions having public, protected, and private access control (no cheating by making everything public), and demonstrate inheritance.

Have a main() function that instantiates an instance of your chosen theme of class with inheritance, and runs that instantiated object through a range of tests demonstrating the class works as intended (likely also prompting the user to input any needed values to configure the attributes of the object).

References

Submission

I'll be looking for the following:

104:oop1:final tally of results (104/104)
*:oop1:no compiler messages, program compiles and runs without issue [52/52]
*:oop1:specified functionality is implemented [52/52]

Additionally:

  • Solutions not abiding by SPIRIT of project will be subject to a 50% overall deduction
  • Solutions not utilizing descriptive why and how COMMENTS will be subject to a 50% overall deduction
  • Solutions not utilizing INDENTATION to promote scope and clarity will be subject to a 50% overall deduction
  • Solutions lacking ORGANIZATION and are not easy to read (within 90 char width) are subject to a 50% overall deduction
haas/spring2024/cprog/projects/oop1.txt · Last modified: 2023/04/10 23:26 by 127.0.0.1