Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members. An
Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are defined using keyword class, with the following syntax: class class_name \{ access_specifier_1: member1; access_specifier_2; member2; _._ object_names; Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, which can either be data or function declarations, and optionally access specifiers. Classes have the same format as plain data structures, except that they can also include functions and have these new things called access specifiers. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights for the members that follow them: - private members of a class are accessible only from within other members of the same class (or from their "friends"). - protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes. - Public members are accessible from anywhere where the object is visible. By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before any other access specifier has private access automatically. - Constructors A class can include a special function called its constructor, which is automatically called whenever a new object of this class is created, allowing the class to initialize member variables or allocate storage. This constructor function is declared just like a regular member function, but with a name that matches the class name and without any return type; not even void. - Overloading constructors Like any other function, a constructor can also be overloaded with different versions taking different parameters: with a different number of parameters and/or parameters of different types. The compiler will automatically call the one whose parameters match the arguments. Exercise 1. [ 10 points] (a) Write a simple class named Vec3d. The class should contain: [-] Three private member variables of type int named m-x, m-y, and m-z; [-] A public member function named setValues() that allows you to set values for mx,my, and mz. [-] A public member function named print() that prints the Point in the following format: mx,my,m [-] A constructor function with default arguments 0,0,0 [-] A public member function named isEqual() that compares two Vec3d objects and returns true if they are equal and false otherwise. See the sample run to produce the corresponding output. [-] A public member function named 'add' that adds a given Vec3d object. See the sample run to produce the corresponding output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started