Question

I'm taking a C++ Object Oriented Programming course right now. My professor only gives us the...

I'm taking a C++ Object Oriented Programming course right now. My professor only gives us the word description of topics and I want to know what it would look like in code form. For example, I understand what classes, operations and inheritance are supposed to do but I want to know what they look like in an example of code.  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Syntax of class-
class class_Name
{
data member;
memeber function;
}
Example-Program for student information using class
Code

#include <iostream>
using namespace std;
class student //creation of class
{
private: // private data member
char name[30];
int rollNo;
  
public:
//member function to get student's information
void getdata(void);
//member function to display student's information
void display(void);
};

//definition of member function getdata(void) outside of the class
void student::getdata(void)
{
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
}

//definition of member function display(void) outside of the class
void student::display(void)
{
  
cout << "Name:"<< name << ",Roll Number:" << rollNo<<"\n";
}

int main()
{
student s1,s2; //creation of two objects objects
s1.getdata(); //calling function
s2.getdata(); //calling function
cout << "Student details:\n";
s1.display(); //calling function
s2.display(); //calling function
return 0;
}
Output-
Enter name: mukesh
Enter roll number: 120
Enter name: ramesh
Enter roll number: 121
Student details:
Name:mukesh,Roll Number:120
Name:ramesh,Roll Number:121

Inheritence-

Syntax of inheritence:

class base_class
{
//body of base class
};
class derived_class:access_mode base_class
{
//body of derived class
}
Program for multiplication of two numbers
code

#include <iostream>
using namespace std;
class base //base class
{
public:
int a;
void getdata() // function taking input the value of a
{
cout << "Enter the value of a = ";
cin >> a;
}
};
class mult : public base //derived class mult
{
private:
int b;
public:
void getdata1()
{
cout << "Enter the value of b = ";
cin >> b;
}
void product() // function for multiplication
{
cout << "Product = " << a * b;
}
};

int main()
{
mult x; //object of derived class
x.getdata();
x.getdata1();
x.product();
return 0;
} //end of program
Output-
Enter the value of a = 3
Enter the value of b = 4
Product = 12

Operator Overloading-
Syntax of Operator Overloading
returntype classname :: operator op (argument)
{
statement;
... ... ...
}
Program for unary operator loading
code

#include <iostream>
#include <conio.h>
using namespace std;
class sample
{
int a,b;
public:
void getdata() // member function definition inside the class
{
cout<<"Enter a and b: ";
cin>>a>>b;
}
void operator -() //operator function as a member function
{
a=-a;
b=-b;
}
void putdata()
{
cout<<"a="<<a<<endl<<"b="<<b<<endl;
}
};

int main()
{
sample x;
x.getdata();
cout<<"Before overloading unary minus operator"<<endl;
x.putdata();
-x;
cout<<"After overloading unary minus operator"<<endl;
x.putdata();
getch();
return 0;
}
Output-
Enter a and b: 5
6
Before overloading unary minus operator
a=5
b=6
After overloading unary minus operator
a=-5
b=-6


Add a comment
Know the answer?
Add Answer to:
I'm taking a C++ Object Oriented Programming course right now. My professor only gives us the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java is an object-oriented programming language that enables us to define classes and to instantiate them...

    Java is an object-oriented programming language that enables us to define classes and to instantiate them into objects. These objects then call each other’s methods to implement the behavior of the application. The Unified Modeling Language (UML) is an object-oriented visual notation to document the design of object-oriented classes. For this discussion, you will practice designing a Java class called Course, drawing a UML class diagram for the Course class, and then implementing the Course class in Java code. Review...

  • 1. Provide a definition for the term abstraction. Give an example in the context of programming...

    1. Provide a definition for the term abstraction. Give an example in the context of programming 2. In object oriented programming, composition and inheritance are both methods for reusing existing code. What are the differences between them. Provide an example where each one can/should be uscd. 3. If you are tasked with developing a program to keep track of NBA teams and games, what objects would you use? For every team, we would like to keep track of the players...

  • I'm having trouble with my Java Homework in which my professor wants us to solve the...

    I'm having trouble with my Java Homework in which my professor wants us to solve the 0-1 Knapsack problem with Dynamic Programming. The code below is what she provided and she requested that we not change any of her existing code but simply add to it. As you can see she gave us the stub file for the knapsack class and the Item class. You are a thief with a knapsack with a carrying capacity of knapsackCapacity pounds. You want...

  • This Java program must use classes and object-oriented design (OOD) as I'm in a more advanced...

    This Java program must use classes and object-oriented design (OOD) as I'm in a more advanced Java class. (Sales Commission Calculator) A large company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of merchandise in a week receives $200 plus 9% of $5000, or a total of $650. You’ve been supplied with a list of the items sold...

  • My professor provided a very vague description of what he wants us to do with the...

    My professor provided a very vague description of what he wants us to do with the example code from the book. I have provided the picture of his description and the code from the book we need to alter. I'm already struggling with this course and I'll probably have to post another question on a different problem I'm stuck on. If you can help it would be highly appreciated. Description Change code for double, string, C-string with appropriate convert constructors...

  • Problem 1 Many applications require us to know the temperature distribution in an object. For example,...

    Problem 1 Many applications require us to know the temperature distribution in an object. For example, this information is important for controlling the material properties, such as hardness, when cooling an object formed from molten metal. In a heat transfer course, the following description of the temperature distribution in a flat, rectangular metal plate is often derived. The temperature is held constant at 7, on three sides, and at 7, on Tix.y) Figure P35 b. Using x=y= I, write a...

  • In c# So far, you have created object-oriented code for individual classes. You have built objects...

    In c# So far, you have created object-oriented code for individual classes. You have built objects from these classes. Last week, you created a UML for new inherited classes and objects. Finally, you have used polymorphism. When you add abstraction to this mix, you have the “4 Pillars of OOP.” Now, you begin putting the pieces together to create larger object-oriented programs. Objectives for the project are: Create OO classes that contain inherited and polymorphic members Create abstracted classes and...

  • Objectives Make the transition to object oriented programming. Go over some of the basics concepts: Classes...

    Objectives Make the transition to object oriented programming. Go over some of the basics concepts: Classes Instance variables Methods Object state The keyword "this", what it does toString method Introduction Phase 1 Review the following Java programs as an example of a Java class and client code that uses it: MimicOct.java and UnderTheSea.java. All of you have been working with classes in Java. One of the most important things to know about classes is that they are actually blueprints for objects. They basically...

  • Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming....

    Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one of the biggest goals is "code reuse". Inheritance accomplishes this. In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct. Constructor implementations must use an initialization list. Please review the book and the online content on these issues so you know how it works...

  • I am taking a computer science course and my professor teaches much too fast. I am...

    I am taking a computer science course and my professor teaches much too fast. I am feeling so overwhelmed and lost. Very worried as well, as I am trying my very hardest in this class but just cant seem to grasp it. I have just been given this assignment.... Can someone explain what I am supposed to do? Is there one class? two? are there supposed to be multiple files? :( thanks! Write a Java class called a Circle that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT